I have 2 data frames with the same column names, but different numbers of rows. The first data frame (a) looks similar to this:
a = data.frame("Site"=c(1,2,3,4,7,9,10,11,13,14),
"v1"=c(0,0,0,0,0,0,0,0,0,0),
"v2"=c(0,0,0,0,NA,NA,NA,0,0,0),
"v3"=c(0,0,0,NA,0,NA,0,0,0,0),
"v4"=c(0,0,0,0,0,0,0,0,NA,NA),
"v5"=c(0,0,0,0,0,NA,0,NA,0,0))
Note: sites 5, 6, 8, and 12 are missing purposefully.
The second data frame (b) looks something like this:
b = data.frame("Site"=c(2,3,4,7,10,14),
"v1"=c(1,NA,2,1,NA,NA),
"v2"=c(1,1,NA,NA,NA,NA),
"v3"=c(NA,1,NA,NA,NA,1),
"v4"=c(1,NA,4,1,NA,NA),
"v5"=c(1,NA,2,1,1,3))
What I want to achieve is this:
desired = data.frame("Site"=c(1,2,3,4,7,9,10,11,13,14),
"v1"=c(0,1,0,2,1,0,0,0,0,0),
"v2"=c(0,1,1,0,NA,NA,NA,0,0,0),
"v3"=c(0,0,1,NA,0,NA,0,0,0,1),
"v4"=c(0,1,0,4,1,0,0,0,NA,NA),
"v5"=c(0,1,0,2,1,NA,1,NA,0,3))
Where I "inject" (I'm sure there's a better term) the data from data frame b into data frame a, however I'd like to replace any NAs from b with zeros and keep the NAs from a as they are.
I found and have tried this code:
cols <- colnames(a)[colnames(a) %in% colnames(b)]
rows <- rownames(a)[rownames(a) %in% rownames(b)]
a[rows, cols] <- b[rows, cols]
But it brings the NAs along with it. I considered replacing the NAs with zeros first, but even then it would erase the NAs I currently have in data frame a that I want to keep.
Perhaps a for loop or something in tidyverse is the way to go, but I don't even know where to begin with those. Any help would be much appreciated!