Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中按行操作纵向数据有困难_R - Fatal编程技术网

在R中按行操作纵向数据有困难

在R中按行操作纵向数据有困难,r,R,我在处理纵向数据时遇到了一些问题:我的数据集由每行一个唯一的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

我在处理纵向数据时遇到了一些问题:我的数据集由每行一个唯一的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

data1这取决于数据的结构。特别是,从第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"))
  ))
}
然后我们可以提取日期,了解上面的“组”,以及如何到达达到正常状态的实际日期列:

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   
日期
[1] “6/18/12”“7/5/12”“9/4/12”“9/22/12”不适用

提取其他信息与此类似,因为列索引可以如上所述进行计算。

签出apply(),它将对data.frame的每一行或每一列应用一个函数,具体取决于为apply()函数提供的是1还是2。为了清楚起见,如果您将您所期望的作为这一小部分行的最终数据帧,这可能会有所帮助。data2似乎与data1不匹配。例如,data1中的第1行不包含日期7/5/12。(修复了data2,谢谢)。感谢您在编码方面的出色指导。仍在努力理解解决方案,但看起来各部分都在这里。不确定如何创建一个函数,该函数将正常接受四种组合中的任何一种。似乎我必须使用“集合”逻辑(即测试x是否是我感兴趣的四个向量的并集的成员),但我发现集合运算符在R中有点困难。发现集合包和区间包,但使用起来并不简单。
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
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