I have the following data example and code:
lt1 <- list(df1 <- data.frame(V1 = c("a", "b"),
V2 = c("b", "c"),
V3 = c(1, 2)),
df2 <- data.frame(V1 = c("x", "y"),
V2 = c("x", "z"),
V3 = c(1, 2)))
lvls_func <- function(x) {
x[1:2] %>%
unlist() %>%
unique() %>%
sort()
}
lt_lvls <- lapply(lt1, lvls_func)
complete_func <- function(x) {
tidyr::complete(x[1] = factor(x[1], levels = lt_lvls),
x[2] = factor(x[2], levels = lt_lvls),
x[3] = x[3],
fill = list(x[3] = 0))
}
lt1_final <- lapply(lt1, complete_func)
I have difficulty building my complete_func().
I getting this error when I run my complete_func()
Error: unexpected '=' in:
"complete_func <- function(x) {
tidyr::complete(x[1] ="
In my final list lt1_final I expect this output:
lt1_final <- list(df1 <- data.frame(V1 = c("a", "b", "a", "a", "b", "b", "c", "c", "c"),
V2 = c("b", "c", "a", "c", "b", "a", "a", "b", "c"),
V3 = c(1, 2, 0, 0, 0, 0, 0, 0, 0)),
df2 <- data.frame(V1 = c("x", "y", "x", "x", "y", "y", "z", "z", "z"),
V2 = c("x", "z", "y", "z", "y", "x", "z", "x", "y"),
V3 = c(1, 2, 0, 0, 0, 0, 0, 0, 0)))
Thanks all help