两个数据帧中的NAs的条件替换 [英] Conditional replacement of NAs in two dataframes R
问题描述
可能简单但棘手的问题,特别是对于较大的数据集。给定两个相同尺寸的数据框( df1
, df2
):
Probably simple but tricky question especially for larger data sets. Given two dataframes (df1
,df2
) of equal dimensions as below:
head(df1)
a b c
1 0.8569720 0.45839112 NA
2 0.7789126 0.36591578 NA
3 0.6901663 0.88095485 NA
4 0.7705756 0.54775807 NA
5 0.1743111 0.89087819 NA
6 0.5812786 0.04361905 NA
head(df2)
a b c
1 0.21210312 0.7670091 NA
2 0.19767464 0.3050934 1
3 0.08982958 0.4453491 2
4 0.75196925 0.6745908 3
5 0.73216793 0.6418483 4
6 0.73640209 0.7448011 5
如果可以在 if(all(is.na(df1))
中找到所有列,在这种情况下 c
,转到 df2
并将匹配列( c
)中的所有值设置为 NAs
。
How can one find all columns where if(all(is.na(df1))
, in this case c
, go to df2
and set all values in matching column (c
) to NAs
.
期望的输出
head(df3)
a b c
1 0.21210312 0.7670091 NA
2 0.19767464 0.3050934 NA
3 0.08982958 0.4453491 NA
4 0.75196925 0.6745908 NA
5 0.73216793 0.6418483 NA
6 0.73640209 0.7448011 NA
我的实际数据框超过 140000
列。
My actual dataframes have more than 140000
columns.
推荐答案
我们可以在否定的逻辑矩阵(是)上使用
),否定(! colSums
na(df1))
vector',使0个非NA元素变为TRUE,所有其他FALSE,使用此对'df2'的列进行子集并将其分配给NA。
We can use colSums
on the negated logical matrix (is.na(df1)
), negate (!) the
vector` so that 0 non-NA elements becomes TRUE and all others FALSE, use this to subset the columns of 'df2' and assign it to NA.
df2[!colSums(!is.na(df1))] <- NA
df2
# a b c
#1 0.21210312 0.7670091 NA
#2 0.19767464 0.3050934 NA
#3 0.08982958 0.4453491 NA
#4 0.75196925 0.6745908 NA
#5 0.73216793 0.6418483 NA
#6 0.73640209 0.7448011 NA
或另一个选项是循环查看列,并检查是否全部
e创建一个逻辑向量,用于对'df2'的列进行子集并将其分配给NA
Or another option is to loop over the columns and check whether all
the elements are NA to create a logical vector for subsetting the columns of 'df2' and assigning it to NA
df2[sapply(df1, function(x) all(is.na(x)))] <- NA
如果这些是大数据集,另一个选项将是从
data.table
If these are big datasets, another option would be set
from data.table
(should be more efficient as this does the assignment in place)
library(data.table)
setDT(df2)
j1 <- which(sapply(df1, function(x) all(is.na(x))))
for(j in j1){
set(df2, i = NULL, j = j, value = NA)
}
这篇关于两个数据帧中的NAs的条件替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!