I have two dataframes, df1 and df2, one is complete while the other has some NA values. For example:
我有两个数据帧,df1和df2,一个是完整的,而另一个有一些NA值。例如:
df1 <- data.frame( id=c(1,2,NA,NA,5,NA), value=c(NA,10,50,30,NA,60))
df2 <- data.frame( id2=c(10,20,30,40,50,60), value2=c(15,25,35,45,55,65))
I want to transfer the NA values from df1
to df2
so that df2
will be:
我想将NA值从df1传输到df2,这样df2将是:
> df2
id2 value2
1 10 NA
2 20 25
3 NA 35
4 NA 45
5 50 NA
6 NA 65
2 个解决方案
#1
1
One possibility using is.na()
to find the position of NA
in df1
(TRUE
indicating missing element):
使用is.na()在df1中找到NA的位置的一种可能性(TRUE表示缺少元素):
idx <- is.na(df1)
idx
id value
[1,] FALSE TRUE
[2,] FALSE FALSE
[3,] TRUE FALSE
[4,] TRUE FALSE
[5,] FALSE TRUE
[6,] TRUE FALSE
Then we can use idx
to replace values in df2
by NA
:
然后我们可以使用idx用NA替换df2中的值:
df2[idx] <- NA
df2
id2 value2
1 10 NA
2 20 25
3 NA 35
4 NA 45
5 50 NA
6 NA 65
Of course, this solution only works when the dimension of df1
and df2
are similar.
当然,该解决方案仅在df1和df2的维度相似时才有效。
#2
0
Following code gives the desired output
以下代码给出了所需的输出
df2$id2[is.na(df1$id)]<-NA
df2$value2[is.na(df1$value)]<-NA
This finds the rows having NA value for each column in the first data set and replaces them with NA in the second dataset.
这将查找第一个数据集中每列具有NA值的行,并将其替换为第二个数据集中的NA。
#1
1
One possibility using is.na()
to find the position of NA
in df1
(TRUE
indicating missing element):
使用is.na()在df1中找到NA的位置的一种可能性(TRUE表示缺少元素):
idx <- is.na(df1)
idx
id value
[1,] FALSE TRUE
[2,] FALSE FALSE
[3,] TRUE FALSE
[4,] TRUE FALSE
[5,] FALSE TRUE
[6,] TRUE FALSE
Then we can use idx
to replace values in df2
by NA
:
然后我们可以使用idx用NA替换df2中的值:
df2[idx] <- NA
df2
id2 value2
1 10 NA
2 20 25
3 NA 35
4 NA 45
5 50 NA
6 NA 65
Of course, this solution only works when the dimension of df1
and df2
are similar.
当然,该解决方案仅在df1和df2的维度相似时才有效。
#2
0
Following code gives the desired output
以下代码给出了所需的输出
df2$id2[is.na(df1$id)]<-NA
df2$value2[is.na(df1$value)]<-NA
This finds the rows having NA value for each column in the first data set and replaces them with NA in the second dataset.
这将查找第一个数据集中每列具有NA值的行,并将其替换为第二个数据集中的NA。