我有一个像这样的广泛数据集:
headers <- c( "student_id",
"w4_1_count", "w4_1_date" ,
"w4_2_count", "w4_2_date",
"w5_1_count", "w5_1_date",
"w5_2_count", "w5_2_date")
row1 <- c(100,0,NA,0,NA,1,"4/04/2019",0, NA)
row2 <- c(101,1,"25/03/2019",0,NA,0,NA,1,"6/04/2019")
row3 <- c(102, 0,NA,0,NA,1,"3/04/2019",0, NA)
df <- as.data.frame(rbind(row1,row2,row3))
colnames(df) <- headers
我想把它变成一个像这样的长数据集:
预期输出:
如何在R中做到这一点? 我尝试了以下代码,但不知道如何计算类型:
require(data.table)
melt(setDT(df),
measure=patterns("count$", "date$"),
value.name=c("count", "date"))