【发布时间】:2020-11-11 23:46:15
【问题描述】:
我有一个宽格式的纵向数据,我正在尝试将其转换为长格式:
#I have a wide data which look like this:
dat_wide <- read.table(text="
cid dyad f1 f2 op2 ed1 junk
1 2 0 0 4 5 0.876
1 5 0 1 4 4 0.765
", header=TRUE)
#I want to convert it to long like this:
dat_long <- read.table(text="
cid dyad f op ed junk Visit
1 2 0 NA 5 0.876 1
1 2 0 4 NA 0.876 2
1 5 0 NA 4 0.765 1
1 5 1 4 NA 0.765 2
", header=TRUE)
#R code I was trying:
dat_l2 = reshape(dat_wide,idvar='cid', varying=list(c('f1','f2'), 'op2','ed1'),
#timevar='Visit',
times=c(1,2),
v.names=c('f','op','ed'),
direction='long')
#gives error:Error in reshape(merge_wide1, idvar = "cid", varying = c("f1", : length of 'v.names' does not evenly divide length of 'varying'
类似于Converting data from wide to long (using multiple columns)
我的数据的不同之处在于:我有一些变量只记录了更少的时间点。例如,变量“f”是从时间 1 和时间 2 记录的,但变量“op”仅记录时间 2(即 op2)& 变量“ed”仅记录时间 1(即 ed1) 头部(数据)
【问题讨论】: