[英]conditional loop in R dataframe
I am trying to get this done in R. The cells in columns 2,3 and 4 contain numbers greater than 10 or 'NA'. 我正在尝试在R中完成此操作。第2、3和4列中的单元格包含大于10或'NA'的数字。 Since this is already filtered data, I want to crosscheck with cells in columns 5,6 and 7. The datapoint in columns 5, 6 or 7 contain some alphabetical annotations.
由于这已经是已过滤的数据,因此我想对5,6和7列中的单元格进行交叉检查。5、6或7列中的数据点包含一些字母注释。 If the cells in columns 2,3 or 4 contain 'NA', then I want to convert the corresponding cell values in columns 5, 6 or 7 as 'N' or 'NA'.
如果第2、3或4列中的单元格包含“ NA”,那么我想将第5、6或7列中的对应单元格值转换为“ N”或“ NA”。
My sample data is as follows: 我的样本数据如下:
ID S1 S2 S3 S4 S5 S6
M1 11 20 NA C C C
M2 NA 123 21 T T R
M3 NA NA 27 A A M
M4 65 23 NA G G C
M5 12 NA 13 T G C
My desired output is:: 我想要的输出是:
ID S1 S2 S3 S4 S5 S6
M1 11 20 NA C C N
M2 NA 123 21 T N R
M3 NA NA 27 N N M
M4 65 23 NA G G N
M5 12 NA 13 T N C
Thanks in advance. 提前致谢。 Jerry
杰瑞
You could try this: 您可以尝试以下方法:
library(data.table)
data <- as.data.table(list(ID=c("M1","M2"), S1=c(11,NA), s4=c("C", "T")))
# ID S1 s4
#1: M1 11 C
#2: M2 NA T
data[, s4 := ifelse(is.na(S1), NA, s4)]
# ID S1 s4
#1: M1 11 C
#2: M2 NA NA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.