在函数中包装dplyr过滤器会导致“错误:结果的长度必须为4803,而不是3". [英] Wrapping dplyr filter in function results in "Error: Result must have length 4803, not 3"

查看:46
本文介绍了在函数中包装dplyr过滤器会导致“错误:结果的长度必须为4803,而不是3".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习R进行数据分析,并使用 Kaggle数据集.遵循电影推荐脚本可以工作,但是当我尝试通过使 dplyr 代码通用化,使其成为一个函数,我得到一个错误:

I'm learning R for data analysis and using this Kaggle dataset. Following the movie recommendation script works, but when I try to generalize a dplyr code by making it a function I get an error:

我已经尝试对一些问题进行故障排除.看起来代码停在了 filter mutate 函数上.

I've tried troubleshooting some. It looks like the code stops at the filter and mutate functions.

以下工作并给出了预期的输出.

The following works and gives the expected output.

genres <- df %>%
  filter(nchar(genres)>2) %>%
  mutate(
    separated = lapply(genres, fromJSON)
  ) %>%
  unnest(separated, .name_repair = "unique") %>%
  select(id, title, keyword = name) %>%
  mutate_if(is.character, factor)

将该代码包装在函数中会导致错误消息:

Wrapping that code in a function results in an error message:

make_df <- function(list_df){
  df %>%
  filter(nchar(list_df)>2) %>%
  mutate(
    separated = lapply(list_df, fromJSON)
  ) %>%
  unnest(separated, .name_repair = "unique") %>%
  select(id, title, keyword = name) %>%
  mutate_if(is.character, factor)
}

预期结果:

> head(genres)
# A tibble: 6 x 3
#      id title                                    keyword        
#   <dbl> <fct>                                    <fct>          
# 1 19995 Avatar                                   Action         
# 2 19995 Avatar                                   Adventure      
# 3 19995 Avatar                                   Fantasy        
# 4 19995 Avatar                                   Science Fiction
# 5   285 Pirates of the Caribbean: At World's End Adventure      
# 6   285 Pirates of the Caribbean: At World's End Fantasy 

实际结果:

> make_df(genres)
#  Error: Result must have length 4803, not 3 
# --- Traceback ---
# 12. stop(structure(list(message = "Result must have length 4803, not 3", 
#     call = NULL, cppstack = NULL), class = c("Rcpp::exception", 
#     "C++Error", "error", "condition"))) 
# 11. filter_impl(.data, quo) 
# 10. filter.tbl_df(., nchar(list_df) > 2) 
# 9. filter(., nchar(list_df) > 2) 
# 8. function_list[[i]](value) 
# 7. freduce(value, `_function_list`) 
# 6. `_fseq`(`_lhs`) 
# 5. eval(quote(`_fseq`(`_lhs`)), env, env) 
# 4. eval(quote(`_fseq`(`_lhs`)), env, env) 
# 3. withVisible(eval(quote(`_fseq`(`_lhs`)), env, env)) 
# 2. df %>% filter(nchar(list_df) > 2) %>% mutate(separated = lapply(list_df, 
#     fromJSON)) %>% unnest(separated, .name_repair = "unique") %>% 
#     select(id, title, keyword = name) %>% mutate_if(is.character, 
#     factor) 
# 1. make_df(genres) 

不带过滤线的实际结果:

Actual results without filter line:

> make_df(genres)
#  Error: Argument 'txt' must be a JSON string, URL or file. 
# 15. base::stop(..., call. = FALSE) 
# 14. stop("Argument 'txt' must be a JSON string, URL or file.") 
# 13. FUN(X[[i]], ...) 
# 12. lapply(list_df, fromJSON) 
# 11. mutate_impl(.data, dots, caller_env()) 
# 10. mutate.tbl_df(., separated = lapply(list_df, fromJSON)) 
# 9. mutate(., separated = lapply(list_df, fromJSON)) 
# 8. function_list[[i]](value) 
# 7. freduce(value, `_function_list`) 
# 6. `_fseq`(`_lhs`) 
# 5. eval(quote(`_fseq`(`_lhs`)), env, env) 
# 4. eval(quote(`_fseq`(`_lhs`)), env, env) 
# 3. withVisible(eval(quote(`_fseq`(`_lhs`)), env, env)) 
# 2. df %>% mutate(separated = lapply(list_df, fromJSON)) %>%  unnest(separated, 
#      .name_repair = "unique") %>% select(id, title, keyword = name) %>% 
#      mutate_if(is.character, factor) 
# 1. make_df(genres) 

推荐答案

您的问题实际上与整洁的编程有关.我指的是 rlang语法

Your problem is actually connected to tidy programming. I am referring to rlang syntax

您只需要在代码中添加{{}}.比它完美运行.

You just need to add the {{ }} in your code. Than it runs perfectly.

您可以使用以下方法解决此问题:

You can solve this with:

make_df <- function(list_df){
  df %>%
    filter(nchar({{ list_df }})>2) %>%
    mutate(
      separated = lapply({{ list_df }}, fromJSON)
    ) %>%
    unnest(separated, .name_repair = "unique") %>%
    select(id, title, keyword = name) %>%
    mutate_if(is.character, factor)
}

比您可以执行(确保删除您环境中的genres对象,否则请给他一个带有您以前的结果而不是字符串的小标题):

Than you can execute (make sure you delete the genres object in your environment, otherwise you give him a tibble with your former result instead of a string):

make_df(genres)

输出为:

# A tibble: 12,160 x 3
       id title                                    keyword        
    <dbl> <fct>                                    <fct>          
 1  19995 Avatar                                   Action         
 2  19995 Avatar                                   Adventure      
 3  19995 Avatar                                   Fantasy        
 4  19995 Avatar                                   Science Fiction
 5    285 Pirates of the Caribbean: At World's End Adventure      
 6    285 Pirates of the Caribbean: At World's End Fantasy        
 7    285 Pirates of the Caribbean: At World's End Action         
 8 206647 Spectre                                  Action         
 9 206647 Spectre                                  Adventure      
10 206647 Spectre                                  Crime          
# ... with 12,150 more rows

这篇关于在函数中包装dplyr过滤器会导致“错误:结果的长度必须为4803,而不是3".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆