[英]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. 此正则表达式将以下匹配项替换为0
: NA
后跟一个逗号和一个数字,而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.