I have this dataframe:
freq 1 2 3 4
1 100 10 20 30 40
2 100 0.50 1.00 1.5 2
3 200 50 60 70 80
4 200 2.5 3 3.5 4
I want to turn this dataframe in this way:
freq 1 2
1 100 10 0.50
2 100 20 1
3 100 30 1.50
4 100 40 2
5 200 50 2.50
6 200 60 3
7 200 70 3.50
8 200 80 4
How can i do that?
I need something that runs for a generic dataframe, not only this case.
We can try
i1 <- c(TRUE, FALSE)
data.frame(freq = rep(df1$freq, each =(ncol(df1)-1)/2),
`1` = c(t(df1[i1,-1])), `2` = c(t(df1[!i1,-1])), check.names=FALSE)
# freq 1 2
#1 100 10 0.5
#2 100 20 1.0
#3 100 30 1.5
#4 100 40 2.0
#5 200 50 2.5
#6 200 60 3.0
#7 200 70 3.5
#8 200 80 4.0
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments