我正在尝试取消堆叠我的数据(从每人多行到一行)。我的代码在模拟数据上运行良好,但是当我将它应用于实际数据时,它会产生错误,而我无法破译它的含义。
这是我的数据:
mbr_id assigned completed score
1: 54 2016-11-10 2016-11-10 0
2: 54 2016-11-21 2016-11-21 7
3: 54 2017-01-26 2017-01-26 11
4: 54 2017-02-23 2017-02-23 12
5: 54 2016-11-21 2016-11-21 1
> summary(testdata)
mbr_id assigned completed
Min. :54 Min. :2016-11-10 Min. :2016-11-10
1st Qu.:54 1st Qu.:2016-11-21 1st Qu.:2016-11-21
Median :54 Median :2016-11-21 Median :2016-11-21
Mean :54 Mean :2016-12-20 Mean :2016-12-20
3rd Qu.:54 3rd Qu.:2017-01-26 3rd Qu.:2017-01-26
Max. :54 Max. :2017-02-23 Max. :2017-02-23
score
Min. : 0.0
1st Qu.: 1.0
Median : 7.0
Mean : 6.2
3rd Qu.:11.0
Max. :12.0
这是我正在运行的代码:
library(data.table)
dcast(setDT(testdata), ID~rowid(mbr_id), value.var= c("assigned","completed","score"), sep="")`
这是我得到的错误:
Error in setDT(dat) :
All elements in argument 'x' to 'setDT' must be of same length
答案 0 :(得分:0)
不完全确定你要做什么......但如果你有一个测试ID,你可以做这样的事情。
mbr_id <- c(54,54,54,54,54)
assigned <- c('2016-11-10','2016-11-21','2017-01-26','2017-02-23','2016-11-21')
completed <- c('2016-11-10','2016-11-21','2017-01-26','2017-02-23','2016-11-21')
score <- c(0,7,11,12,1)
testid <- c(1,2,3,4,5)
testdata <- data.frame(mbr_id, assigned, completed, score, testid)
dcast(testdata, mbr_id ~ testid , value.var = "score", sep="")
我假设您有多个mbr_id,并且所有都有不同的分配。所以你可以用这种方式重塑。
mbr_id <- c(54,54,54,54,54,42,42)
assigned <- c('2016-11-10','2016-11-21','2017-01-26','2017-02-23','2016-11-21','2017-02-23','2016-11-21')
completed <- c('2016-11-10','2016-11-21','2017-01-26','2017-02-23','2016-11-21','2017-02-23','2016-11-22')
score <- c(0,7,11,12,1,20,30)
testid <- c(1,2,3,4,5,4,5)
testdata <- data.frame(mbr_id, assigned, completed, score, testid)
# dcast(testdata, mbr_id ~ testid , value.var = "score", sep="")
wanted_result <- reshape(testdata, direction = "wide", idvar="mbr_id", timevar="testid")