我有一个这样的数据框
df= data.frame(a1 = c(1,2,3), a2 = c(4,5,6), b1 = c(1,2,3), b2= c(4,NaN,6), id = c(1,2,3))
我想得到
id a measure1 b measure2
1 1 a1 1 b1 1
2 2 a1 2 b1 2
3 3 a1 3 b1 3
4 1 a2 4 b2 4
5 2 a2 5 b2 NaN
6 3 a2 6 b2 6
我可以做
df1 = df[, c(1,2,5)]
df2 = df[, c(3,4,5)]
library(reshape2)
df1_long = melt(df1,id.vars= 'id', measure.vars=c("a1", "a2"),
variable.name="a",
value.name="measure1")
df2_long = melt(df2,id.vars= 'id', measure.vars=c("b1", "b2"),
variable.name="b",
value.name="measure2")
df_new = cbind(df1_long, df2_long[, -1])
但是我认为有一种更简单的方法
答案 0 :(得分:3)
在基数R中,您可以使用reshape
函数。然后,您可以根据需要更改列:
transform(reshape(df,1:4,sep="",dir="long"),a_=paste0("a",time),b_=paste0("b",time))
id time a b a_ b_
1.1 1 1 1 1 a1 b1
2.1 2 1 2 2 a1 b1
3.1 3 1 3 3 a1 b1
1.2 1 2 4 4 a2 b2
2.2 2 2 5 NaN a2 b2
3.2 3 2 6 6 a2 b2
答案 1 :(得分:2)
这是相对的,您认为更简单,但是可以选择使用dplyr
和tidyr
:
df %>%
select(id, starts_with("a")) %>%
gather(a, measurement1, -id) %>%
bind_cols(df %>%
select(starts_with("b")) %>%
gather(b, measurement2))
id a measurement1 b measurement2
1 1 a1 1 b1 1
2 2 a1 2 b1 2
3 3 a1 3 b1 3
4 1 a2 4 b2 4
5 2 a2 5 b2 NaN
6 3 a2 6 b2 6