我正在尝试编写一个函数,其中输入是一个数据帧和该特定数据帧中的变量名,该函数中的dplyr表达式必须使用该函数。该函数写为:
nested_list_to_dummy <- function(data_frame,variable){
data_frame %>% select(variable) %>%
mutate(ID_before = row_number()) %>%
unnest() %>%
mutate(ID_after = row_number(), count = 1,
variable = fct_lump(factor(variable), n = min(8,
length(unique(variable))))) %>%
spread(key = variable, value = count, fill = 0) %>%
group_by(ID_before) %>%
summarise_at(vars(colnames(.)[-c(1,2)]), funs(sum)) %>% select(-1) %>%
mutate_all(funs(as.logical))
}
但是,当我调用该函数时,出现以下错误:
nested_list_to_dummy(movies, genres)
Error in .f(.x[[i]], ...) : object 'genres' wasn't found
我该如何以正确的方式做到这一点?
谢谢。