问题描述
#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)
我的数据不同是:我有一些变量只记录了较少的时间点。例如,从时间1和时间2开始都记录了变量'f',但是仅对时间2(即op2)记录了变量'op',仅对时间1(即ed1)记录了变量'ed' 头(数据)
解决方法
您可以使用pivot_longer
中的tidyr
:
tidyr::pivot_longer(dat_wide,cols = f1:ed1,names_to = c('.value','Visit'),names_pattern = '(.*)(\\d+)')
# cid dyad junk Visit f op ed
# <int> <int> <dbl> <chr> <int> <int> <int>
#1 1 2 0.876 1 0 NA 5
#2 1 2 0.876 2 0 4 NA
#3 1 5 0.765 1 0 NA 4
#4 1 5 0.765 2 1 4 NA