简体   繁体   English

将条件中的NA替换为零

[英]Replacing NAs in R with condition to zero

I have this dataframe called mydf . 我有一个名为mydf数据mydf I need to replace the NAs in mydf with (zero) ' 0 ' if meets this condition: if number, NA ; 如果满足以下条件,我需要将mydf中的NAs替换为(零)“ 0 ”:if number, NA ; or NA , number is present; NA ,数字存在; then NA has to be 0 else every other NA's remains as is. 那么NA必须为0否则其他所有NA都保持不变。 The result is shown below. 结果如下所示。

mydf mydf

A     B     C
1,3   1,NA  NA,1
NA,4  0,0   5,NA
NA    NA,NA NA,6

result 结果

   A     B     C
   1,3   1,0  0,1
   0,4  0,0   5,0
   NA   NA,NA 0,6

You can use regular expressions for this task. 您可以为该任务使用正则表达式。

mydf[] <- lapply(mydf, sub, pattern = "NA(?=,[0-9])|(?<=[0-9],)NA", 
                 replacement = "0", perl = TRUE)

mydf
#     A     B   C
# 1 1,3   1,0 0,1
# 2 0,4   0,0 5,0
# 3  NA NA,NA 0,6

This regex replaces the following matches with 0 : NA followed by a comma and a digit, and NA preceded by a digit and a comma. 此正则表达式将以下匹配项替换为0NA后跟一个逗号和一个数字,而NA前跟一个数字和一个逗号。


For this solution, I assume mydf is structured as follows: 对于此解决方案,我假设mydf的结构如下:

mydf <- structure(list(A = c("1,3", "NA,4", "NA"), B = c("1,NA", "0,0", 
"NA,NA"), C = c("NA,1", "5,NA", "NA,6")), .Names = c("A", "B", 
"C"), row.names = c(NA, -3L), class = "data.frame")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM