How to replace some values of one dataframe with another dataframe in R?
I have two large blocks of data with the same columns and rows, but I need to substitute the NA
first one based on the second. For example, it is assumed that the data frame "DF1" is
DF1 <- data.frame(a=c(1,NA,3), b=c(4,NA,6))
and "DF2"
D2 <- data.frame(a=c(NA,2,NA), b=c(3,5,6))
When there is "DF1" NA
, I want to replace "DF1" with "DF2" and create a new "DF3", ie
a b
1 4
2 5
3 6
could you help me?
source to share
This should do the trick:
DF3 <- DF1
replace.bool.matrix <- is.na(DF1)
DF3[replace.bool.matrix] <- DF2[replace.bool.matrix]
Explanation:
We create DF3
which is a copy DF1
. We then create a boolean matrix replace.bool.matrix
that we use to select the values in DF3
to replace, and the values in DF2
to replace them.
It uses a selection of dataframe operations for which there are many tutorials.
source to share
It's much easier with the match () function:
df1 <- data.frame(steps=c(NA,NA,NA,NA,NA,NA,NA,NA),date=c('2012-10-01','2012-10-01','2012-10-01','2012-10-01','2012-10-01','2012-10-01','2012-10-02','2012-10-02'), interval=c(0,5,10,15,20,25,0,5))
df2 <- data.frame(Interval=c(0,5,10,15,20,25),x=c(1.716,0.339,0.132,0.151,0.075,2.094))
if (is.na(df1$steps)==TRUE) df1$steps <- df2$x[match(df1$interval,df2$Interval)]
source to share