DATA=data.frame(SUBJECT=c(1,1,1,2,2,2,3,3,4), TIME=c(1,2,3,1,2,3,1,2,1),
TYPE=c("dog","dog","cat","cat","dog","cat","cat","cat","dog"),
FIRST=c("dog","dog","dog","cat","cat","cat","cat","cat","dog"),
LAST=c("cat","cat","cat","cat","cat","cat","cat","cat","dog"),
DOG=c(2,2,2,1,1,1,0,0,1),
CAT=c(1,1,1,2,2,2,2,2,0))
我有'DATA'和' subject ';和";TYPE"one_answers"TIME",我希望创建"FIRST", "LAST", "DOG", "CAT">
"数据"按主题和时间排序。
我希望创建一个变量FIRST,它等于列'TYPE'中报告的第一个值
我希望创建一个变量LAST,它等于列'TYPE'中报告的LAST值
然后我希望创建变量DOG和CAT,它们等于有多少个"狗"one_answers";cat">
library(data.table)
DT <- as.data.table(DATA[,1:3])
# step 1, get the first/last
DT[, c("FIRST", "LAST") := .(first(TYPE), last(TYPE)), by = .(SUBJECT) ]
DT
# SUBJECT TIME TYPE FIRST LAST
# <num> <num> <char> <char> <char>
# 1: 1 1 dog dog cat
# 2: 1 2 dog dog cat
# 3: 1 3 cat dog cat
# 4: 2 1 cat cat cat
# 5: 2 2 dog cat cat
# 6: 2 3 cat cat cat
# 7: 3 1 cat cat cat
# 8: 3 2 cat cat cat
# 9: 4 1 dog dog dog
# step 2, bring in the counts
counts <- dcast(DT[, .(n = .N), by = .(SUBJECT, TYPE)], SUBJECT ~ TYPE, value.var = "n", fill = 0)
counts
# SUBJECT cat dog
# <num> <int> <int>
# 1: 1 1 2
# 2: 2 2 1
# 3: 3 2 0
# 4: 4 0 1
counts[DT, on = .(SUBJECT)]
# SUBJECT cat dog TIME TYPE FIRST LAST
# <num> <int> <int> <num> <char> <char> <char>
# 1: 1 1 2 1 dog dog cat
# 2: 1 1 2 2 dog dog cat
# 3: 1 1 2 3 cat dog cat
# 4: 2 2 1 1 cat cat cat
# 5: 2 2 1 2 dog cat cat
# 6: 2 2 1 3 cat cat cat
# 7: 3 2 0 1 cat cat cat
# 8: 3 2 0 2 cat cat cat
# 9: 4 0 1 1 dog dog dog