逐行分析二进制数据集的值

Analyse binary dataset row-wise regarding value after a one

我想计算一个人是否从一年活到下一年。 0 表示它死亡,1 表示它存活。数据集由不同的年份(2007 年到 2020 年)组成,计算应该从 2008 年开始。我只希望 R 使用我拥有的一部分数据。

我的数据集如下所示:

the first 17 rows of my data set

> ID 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
   1   0    1    0    0    0    0    0    0    0    0    0     0   0   0  
   3   0    1    1    1    0    0    0    0    0    0    0     0   0   0 
   4   0    1    1    1    0    0    0    0    0    0    0     0   0   0
   9   0    1    0    0    0    0    0    0    0    0    0     0   0   0
   24  0    0    1    1    1    1    1    1    1    1    1     1   1   0
  ...

我总共有 1,121 个条目,总共 16 个列。

我想让R从2008年的第一行开始,看看有没有1。如果有 1,我希望 R 查看下一列 (2009),看看是否还有 1(应该给我 1 作为输出)或 0(应该给我 0 作为输出)。如果没有 1,我希望 R 检查下一列,直到找到带有 1 的年份,然后它应该检查下一列,如上所述。在找到 1 并进行检查后,它应该忽略剩余的列并移至下一行并重复该过程。输出应保存在新列中。

我尝试了 for 循环和 if else 语句以及 ifelse, if ...

我最接近目标的是使用以下代码

for(x in foal_fates_2)) {
  if (foal_fates_2$`2008`=="1" && foal_fates_2$`2009` =="1") {
    print("1")
  } else if (foal_fates_2$`2008`== "1" && foal_fates_2$`2009` =="0") {
    print("0")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="1" && foal_fates_2$`2010` == "1"){
    print("1")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="1" && foal_fates_2$`2010`== "0") {
    print("0")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="1" && 
             foal_fates_2$`2011`=="1"){
    print("1")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="1" && 
             foal_fates_2$`2011`=="0"){
    print("0")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="1" && foal_fates_2$`2012`=="1"){
    print("1")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="1" && foal_fates_2$`2012`=="0"){
    print("0")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="0" && foal_fates_2$`2012`=="1" && foal_fates_2$`2013`=="1"){
    print("1")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="0" && foal_fates_2$`2012`=="1" && foal_fates_2$`2013`=="0"){
    print("0")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="0" && foal_fates_2$`2012`=="0" && foal_fates_2$`2013`=="1" &&
             foal_fates_2$`2014`== "1"){
    print("1")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="0" && foal_fates_2$`2012`=="0" && foal_fates_2$`2013`=="1" &&
             foal_fates_2$`2014`=="0"){
    print("0")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="0" && foal_fates_2$`2012`=="0" && foal_fates_2$`2013`=="0" &&
             foal_fates_2$`2014`== "1" && foal_fates_2$`2015`=="1"){
    print("1")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
            foal_fates_2$`2011`=="0" && foal_fates_2$`2012`=="0" && foal_fates_2$`2013`=="0" &&
            foal_fates_2$`2014`== "1" && foal_fates_2$`2015`=="0"){
    print("0")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="0" && foal_fates_2$`2012`=="0" && foal_fates_2$`2013`=="0" &&
             foal_fates_2$`2014`== "0" && foal_fates_2$`2015`=="1" && foal_fates_2$`2016` =="1"){
    print("1")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="0" && foal_fates_2$`2012`=="0" && foal_fates_2$`2013`=="0" &&
             foal_fates_2$`2014`== "0" && foal_fates_2$`2015`=="1" && foal_fates_2$`2016` =="0"){
    print("0")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="0" && foal_fates_2$`2012`=="0" && foal_fates_2$`2013`=="0" &&
             foal_fates_2$`2014`== "0" && foal_fates_2$`2015`=="0" && foal_fates_2$`2016` =="1" &&
             foal_fates_2$`2017`=="1"){
    print("1")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="0" && foal_fates_2$`2012`=="0" && foal_fates_2$`2013`=="0" &&
             foal_fates_2$`2014`== "0" && foal_fates_2$`2015`=="0" && foal_fates_2$`2016` =="1" &&
             foal_fates_2$`2017`=="0"){
    print("0")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="0" && foal_fates_2$`2012`=="0" && foal_fates_2$`2013`=="0" &&
             foal_fates_2$`2014`== "0" && foal_fates_2$`2015`=="0" && foal_fates_2$`2016` =="0" &&
             foal_fates_2$`2017`=="1" && foal_fates_2$`2018`=="1"){
    print("1")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="0" && foal_fates_2$`2012`=="0" && foal_fates_2$`2013`=="0" &&
             foal_fates_2$`2014`== "0" && foal_fates_2$`2015`=="0" && foal_fates_2$`2016` =="0" &&
             foal_fates_2$`2017`=="1" && foal_fates_2$`2018`=="0"){
    print("0")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="0" && foal_fates_2$`2012`=="0" && foal_fates_2$`2013`=="0" &&
             foal_fates_2$`2014`== "0" && foal_fates_2$`2015`=="0" && foal_fates_2$`2016` =="0" &&
             foal_fates_2$`2017`=="0" && foal_fates_2$`2018`=="1" && foal_fates_2$`2019`=="1"){
    print("1")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="0" && foal_fates_2$`2012`=="0" && foal_fates_2$`2013`=="0" &&
             foal_fates_2$`2014`== "0" && foal_fates_2$`2015`=="0" && foal_fates_2$`2016` =="0" &&
             foal_fates_2$`2017`=="0" && foal_fates_2$`2018`=="1" && foal_fates_2$`2019`=="0"){
    print("0")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="0" && foal_fates_2$`2012`=="0" && foal_fates_2$`2013`=="0" &&
             foal_fates_2$`2014`== "0" && foal_fates_2$`2015`=="0" && foal_fates_2$`2016` =="0" &&
             foal_fates_2$`2017`=="0" && foal_fates_2$`2018`=="0" && foal_fates_2$`2019`=="1" &&
             foal_fates_2$`2020`=="1"){
    print("1")
  } else if (foal_fates_2$`2008`== "0" && foal_fates_2$`2009` =="0" && foal_fates_2$`2010` =="0" && 
             foal_fates_2$`2011`=="0" && foal_fates_2$`2012`=="0" && foal_fates_2$`2013`=="0" &&
             foal_fates_2$`2014`== "0" && foal_fates_2$`2015`=="0" && foal_fates_2$`2016` =="0" &&
             foal_fates_2$`2017`=="0" && foal_fates_2$`2018`=="0" && foal_fates_2$`2019`=="1" &&
             foal_fates_2$`2020`=="0"){
    print("0")
  } 

}

使用此代码 R 至少做了一些事情,结果具有正确的实体数但输出不正确。 R 给了我 0 和 1 但不在正确的位置。意思例如对于前五行,R 给了我结果“0”“0”“0”“1”“0”,但它应该是“0”“1”“1”“1”“0”。至少如果我理解正确的话。我是 R 的新手,所以也许 for loop 和 if else 不是我想做的事情的正确工具。所以,问题是我怎样才能达到我的目标。如果有任何帮助,我将不胜感激。

我会写一个函数应用于每一行。 类似于下面的内容(当然可以更详细,但应该可以完成工作):

numberAfterFirstOne <- function(myRow){
  x <- which(myRow == 1)[1] 
  if (length(x + 1) < length(myRow)) # 
    return(myRow[x + 1])
  else 
    return(NA)
}

解释:

  1. 哪些索引等于一个,就select第一个;如果 none 为 1,则 x 将为 NA。
  2. 如果第一个值之后还有一个值,return它
  3. return NA(也可以是 0 或任何 'key value' 你想要的

这里有一个示例数据集用于测试:

n <- 5
m <- 16
set.seed(1562) # for reproducability
dataset <- as.data.frame(matrix(ncol = m, nrow = n, data = round(runif(m * n, 0, 0.7))))
dataset <- rbind(dataset, rep(0, 16))

  V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16
1  1  0  0  1  0  0  0  1  0   1   0   1   0   0   1   0
2  1  1  0  0  0  1  1  0  0   1   1   0   0   0   1   0
3  0  0  0  0  0  1  0  0  0   0   0   1   0   0   0   1
4  1  0  0  0  0  0  0  0  0   1   0   0   1   0   1   0
5  0  1  1  0  0  1  0  1  0   1   0   1   0   0   1   0
6  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0

然后apply每行的函数numberAfterFirstOne(apply 类似于for-loop,但写和读更整洁)。

apply(dataset, 1, numberAfterFirstOne)
[1]  0  1  0  0  1 NA

这类似于 for-loop 的更笨拙的结构:

result <- c()
for (i in 1:nrow(dataset)){
  result[i] <- numberAfterFirstOne(dataset[i, ])
}

您现在可以将函数调整为 return 您想要的。目前可能有 0、1 或 NA returned,也许您只想要 1 和 0 或 1 和 NA。 if (length(x+1)) 的检查是没有必要的,因为如果索引没有反弹,NA 由 myRow[x+1] 编辑 return,这将使函数更简单。

你也可以修改代码,这样年份也是returned:

colnames(dataset) <- 2007:2020 # name the columns of the example dataset

numberAfterFirstOne <- function(myRow){
  x <- which(myRow == 1)[1]
  return(c(x, myRow[x + 1])) # return the column index + the value
}

result <- apply(dataset, 1, numberAfterFirstOne) #save the result
result[1, ] <- names(dataset)[result[1, ]] # set column index to name of dataset column
     [,1]   [,2]   [,3]   [,4]   [,5]   [,6]
[1,] "2007" "2007" "2012" "2007" "2008" NA  
[2,] "0"    "1"    "0"    "0"    "1"    NA