在R

时间:2019-04-18 06:34:17

标签: r data-cleaning

我有一个像这样的广泛数据集:

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

我想把它变成一个像这样的长数据集:

  • student_id
  • 类型:包含df列名
  • count:计数df计数变量
  • 日期:包含df日期变量

预期输出:

  • student_id | type | count | date
  • 100 | w4_1 | 0 | NA
  • 101 | w4_1 | 1 | 25/03/2019
  • 102 | w4_1 | 0 | NA
  • 100 | w5_1 | 1 | 4/04/2019

如何在R中做到这一点? 我尝试了以下代码,但不知道如何计算类型:

require(data.table)
melt(setDT(df), 
     measure=patterns("count$", "date$"), 
     value.name=c("count", "date"))

0 个答案:

没有答案