匹配R中三列之间的大小写

问题描述

提前感谢您阅读本文的时间。 我有一个看起来像这样的数据框。

data = data.frame(time=c(rep(c(0,1,2),each=3)),parent=c(1,2,3,4,5),offspring= c(NA,NA,5,6,7,8,9))

time parent offspring 
 0     1       NA
 0     2       NA
 0     3       NA
 1     1        4
 1     1        5
 1     2        6
 2     1        7
 2     4        8
 2     5        9

我想创建一个新列“ alpha”并分配 到最后一个时间点(又称时间点2)的后代的值“ 1”。

time parent offspring  alpha
 0     1       NA        NA
 0     2       NA        NA
 0     3       NA        NA
 1     1        4        NA
 1     1        5        NA
 1     2        6        NA
 2     1        7        1
 2     4        8        1
 2     5        9        1

对我来说棘手的部分是下一步。 我想指派那些后代的父母 以及值“ 1”以及其祖父母和 我的数据框看起来像这样。

time parent offspring  alpha
 0     1       NA        1
 0     2       NA        NA
 0     3       NA        NA
 1     1        4        1
 1     1        5        1
 1     2        6        NA
 2     1        7        1
 2     4        8        1
 2     5        9        1

我必须告诉你,我有数千代人。 任何帮助和评论将不胜感激。

解决方法

这可能有帮助:

#Assign `NA` to new column
data$alpha <- NA  
#get the parent value at max time period
parent <- unique(na.omit(data$parent[data$time == max(data$time)]))
#Change those values 1 wherever those value exist (offspring or parent)
data$alpha[data$parent %in% parent | data$offspring %in% parent] <- 1
data

#  time parent offspring alpha
#1    0      1        NA     1
#2    0      2        NA    NA
#3    0      3        NA    NA
#4    1      1         4     1
#5    1      1         5     1
#6    1      2         6    NA
#7    2      1         7     1
#8    2      4         8     1
#9    2      5         9     1
,

如果您有很多世代,则可能需要循环

## set alpha to 1 if time == max(time)
data$alpha <- ifelse(data$time == max(data$time),1,NA)
## initialize inds
inds <- 1
## continue to loop while inds has any values in it
while(length(inds) > 0){
  ## identify points where alpha==NA and the parent is among the parents where alpha == 1 or the offspring are among the parents where alpha == 1
  inds <- which(is.na(data$alpha) & (data$parent %in% data$parent[which(data$alpha == 1)] | data$offspring %in% data$parent[which(data$alpha == 1)])
  ## replace those observations' alpha values with 1
  data$alpha[inds] <- 1
  ## continue to loop back through generations
}