我在使用纵向数据时遇到了一些麻烦:我的数据集包含每行一个唯一ID,后跟一系列访问日期。每次访问时都有3个二分变量的值。
data1 <- structure(list(V1date = structure(c(2L, 1L, 2L, 3L, 4L), .Label = c("1/22/12", "4/5/12", "8/18/12", "9/6/12"), class = "factor"),
V1a = structure(c(1L, 1L, 2L, 1L, 2L), .Label = c("No", "Yes"), class = "factor"),
V1b = structure(c(2L, 1L, 1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"),
V1c = structure(c(1L, 2L, 1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"),
V2date = structure(c(1L, 2L, 4L, 3L, NA), .Label = c("6/18/12", "7/5/12", "9/22/12", "9/4/12"), class = "factor"),
V2a = structure(c(1L, 1L, 1L, 1L, NA), .Label = "Yes", class = "factor"),
V2b = structure(c(1L, 1L, 1L, 1L, NA), .Label = "No", class = "factor"),
V2c = structure(c(1L, 1L, 1L, 1L, NA), .Label = "Yes", class = "factor"),
V3date = structure(c(NA, NA, 1L, NA, 2L), .Label = c("11/1/12", "12/4/12"), class = "factor"),
V3a = structure(c(NA, NA, 1L, NA, 1L), .Label = "Yes", class = "factor"),
V3b = structure(c(NA, NA, 1L, NA, 1L), .Label = "No", class = "factor"),
V3c = structure(c(NA, NA, 2L, NA, 1L), .Label = c("No", "Yes"), class = "factor")),
.Names = c("V1date", "V1a", "V1b", "V1c", "V2date", "V2a", "V2b", "V2c", "V3date", "V3a", "V3b", "V3c"),
class = "data.frame", row.names = c("001", "002", "003", "004", "005"))
data1
V1date V1a V1b V1c V2date V2a V2b V2c V3date V3a V3b V3c
001 4/5/12 No Yes No 6/18/12 Yes No Yes <NA> <NA> <NA> <NA>
002 1/22/12 No No Yes 7/5/12 Yes No Yes <NA> <NA> <NA> <NA>
003 4/5/12 Yes No No 9/4/12 Yes No Yes 11/1/12 Yes No Yes
004 8/18/12 No No No 9/22/12 Yes No Yes <NA> <NA> <NA> <NA>
005 9/6/12 Yes No No <NA> <NA> <NA> <NA> 12/4/12 Yes No No
在三个变量的8种不同可能组合中,4种是“异常”,其余4种是“正常”。每个人都开始出现异常,然后在随后的访问中继续出现异常,或者在以后的访问中解析为正常模式(我忽略了恢复到异常 - 一旦它们正常,它们就是正常的)
我必须在数据框的右侧添加4个新列,表示1)上次完成访问的日期(无论介入的“NAs”,2)ID是否最终解决或保持异常,3 )如果解决了,解决方案模式是什么,4)决议的日期是什么。 NA总是以4个为一组(即没有访问日期,3个变量没有值)并被忽略。
例如,如果模式“yes-yes-no”,“yes-no-yes”,“no-yes-yes”和“yes-yes-yes”是正常的,其余模式都是正常的,结果将是四个额外的列如下;
data2 <- structure(list(
LastVisDate = structure(c(3L, 2L, 3L, 3L, 2L), .Label = c("6/18/12", "12/4/12", "11/1/12", "9/22/12"), class = "factor"),
Resolved = structure(c(2L, 2L, 2L, 2L, 1L), .Label = c("No", "Yes"), class = "factor"),
Pattern = structure(c(1L, 1L, 1L, 1L, NA), .Label = "yny", class = "factor"),
Resdate = structure(c(1L, 2L, 3L, 4L, NA), .Label = c("6/18/12", "7/5/12", "9/4/12", "9/22/12"), class = "factor")),
.Names = c("LastVisDate", "Resolved", "Pattern", "Resdate"),
class = "data.frame", row.names = c("001", "002", "003", "004", "005"))
data2
LastVisDate Resolved Pattern Resdate
001 11/1/12 Yes yny 6/18/12
002 12/4/12 Yes yny 7/5/12
003 11/1/12 Yes yny 9/4/12
004 11/1/12 Yes yny 9/22/12
005 12/4/12 No <NA> <NA>
我花了很多时间在这个项目上,但无法弄清楚如何让R在数据集中向右行进,直到我的停止规则得到满足为止。建议非常感谢。
答案 0 :(得分:1)
这取决于数据的结构。特别是,从第2,6和10列开始有三个值,这些值被传递给确定某人是否“正常”的函数。
这是确定某人是否“正常”的功能。还有其他方法可以写这个。
is.normal <- function(x) {
any(c(
all(x == c("Yes", "Yes", "No")),
all(x == c("Yes", "No", "Yes")),
all(x == c("No", "Yes", "Yes")),
all(x == c("Yes", "Yes", "Yes"))
))
}
我们使用它,适用于适当的列集。这取决于您在问题中指定的确切布局。请注意传递给vapply的列号。这里的结果是一个逻辑矩阵,告诉每个人是否“正常”。
ok <- vapply(c(2,6,10),
function(x) apply(data1[x:(x+2)], 1, is.normal ),
logical(length(data1[,1])))
> ok
[,1] [,2] [,3]
001 FALSE TRUE NA
002 FALSE TRUE NA
003 FALSE TRUE TRUE
004 FALSE TRUE NA
005 FALSE NA FALSE
现在发现每个人第一次变得“正常”,如果有的话。通过检查,除了最后一个,每个人都是2,谁仍然不正常。 if
用于防止Inf
在未达到常规时从min
返回值。
date.ind <- apply(ok, 1,
function(x) {
y <- which(x)
if (length(y)) min(y) else NA
}
)
> date.ind
001 002 003 004 005
2 2 2 2 NA
然后我们可以提取日期,知道上面的“组”,以及如何到达实现常态的实际日期列:
dates <- vapply(seq_along(date.ind),
function(x) if (is.na(date.ind[x])) as.character(NA) else as.character(data1[x,date.ind[x]*4-3]),
character(1)
)
> dates
[1] "6/18/12" "7/5/12" "9/4/12" "9/22/12" NA
提取其他信息是类似的,因为列索引可以按上述方式计算。