I've looked at the discussion What is the difference between as.tibble(), as_data_frame(), and tbl_df()? to figure out why a replace_na
function (shown below) works on data frames but not on tibbles. Could you help me understand why it doesn't work on tibbles? How can the function be modified so it can work for both data.frame
and tibble
?
Data
library(dplyr)
#dput(df1)
df1 <- structure(list(id = c(1, 2, 3, 4), gender = c("M", "F", NA, "F"
), grade = c("A", NA, NA, NA), age = c(2, NA, 2, NA)), row.names = c(NA,
-4L), class = c("tbl_df", "tbl", "data.frame"))
#dput(df2)
df2 <- structure(list(id = c(1, 2, 3, 4), gender = c("M", "F", "M",
"F"), grade = c("A", "A", "B", "NG"), age = c(22, 23, 21, 19)), row.names = c(NA,
-4L), class = c("tbl_df", "tbl", "data.frame"))
replace function
replace_na <- function(df_to, df_from) {
replace(df_to, is.na(df_to), df_from[is.na(df_to)])
}
Usage
replace_na(df1,df2)
Error: Must use a vector in
[
, not an object of class matrix.Call
rlang::last_error()
to see a backtraceCalled from: abort(error_dim_column_index(j))
However; coercing the arglist
to data frame produces the desired output as shown below.
replace_na(as.data.frame(df1), as.data.frame(df2))
# id gender grade age
# 1 1 M A 2
# 2 2 F A 23
# 3 3 M B 2
# 4 4 F NG 19
Thank you.