3

我在处理纵向数据时遇到了一些麻烦:我的数据集每行包含一个唯一 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 向右行进穿过数据集。建议非常感谢。

4

1 回答 1

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   

提取其他信息是类似的,因为列索引可以如上计算。

于 2012-12-16T01:42:58.960 回答