Hi, with the help @FJCC , we use the function coalesce we succeed in solving the problem in former post like the following.
Maybe the dataframe merge? - General - Posit Community (rstudio.com)
Recently, new question and thought comes up. When there is character in dataframe, error comes, when practice R.
library(purrr)
library(dplyr)
c1<-c(2,NA)
c2<-c(NA,5)
D1<-data.frame(c1,c2)
c3<-c(NA,3)
c4<-c("tree",NA)
D2<-data.frame(c3,c4)
D4 <- map2_dfc(D1, D2, ~coalesce(.x, .y))
D4
Error in `map2()`:
ℹ In index: 2.
ℹ With name: c2.
Caused by error in `coalesce()`:
! Can't combine `..1` <double> and `..2` <character>.
Run `rlang::last_error()` to see where the error occurred.
What's more, the function coalesce seems works only when two dataframes. If there are more than two dataframes, like the example data, four dataframes. And also have characters. Is there any solution?
c1<-data.frame(a1=c(2,NA,NA),
a2=c(6,NA,NA),
a3=c(NA,9,NA))
c2<-data.frame(b1=c(NA,NA,10),
b2=c(NA,NA,NA),
b3=c("tree",NA,NA))
c3<-data.frame(c1=c(NA,5.1,NA),
c2=c(NA,5.6,NA),
c3=c(NA,NA,NA))
c4<-data.frame(d1=c(NA,NA,NA),
d2=c(NA,NA,9.2),
d3=c(NA,NA,88))
Any help would be appreciated!!!