R:DPLYR包:当调用自定义函数时,bind_rows失败 [英] R: DPLYR package: bind_rows failing when calls a custom function
问题描述
使用DPLYR和TIDYR,我试图创建一个数据集的整洁版本,根据某些列的数据,可以丢失行。我创建了一个函数,它返回缺少的行(通过使用默认数据创建它们)在一个新的tbl_df(data.frame)(我进行单元测试,它可以与特定数据一起使用)。
但是,当从'bind_rows'调用它时,我收到以下错误:
数据框中的错误(a,b,c,...:
对象'A'没有找到。
例如,我的数据如下所示:
ABCDE ...
a1 b1 c1 d1 e1 ...
a2 b2 c2 d2 e2 ...
...
我的代码如下所示:
data_tidy< - data %>%
<某些其他功能可以像'mutuate','filter'等清理>%>%
brind_rows(myCustomFunction ,B,C,D,E ...))
任何想法我做错了什么?我还是新来的,DPLYR / TIDYR ...
注意:如果我rem最后一次调用'bind_rows',表格按照预期的正确的A,B,C等列进行清理。我也使用这个特定场景中的'for'循环,我知道这可能不是最佳的,但是现在我将使用这个版本,所以我可以让它工作,然后尝试优化我的代码(或矢量化)。 p>
谢谢!
在您致电 foo%>%brind_rows(myCustomFunction(A,B,C,D,E ...))
, myCustomFunction(A,B,C,D, 。)
被称为普通的R函数,而我认为你希望它在一个dplyr函数的上下文中被评估,如 mutate(x = myCustomFunction A,B,C,D,E ...))
其中参数 A,B,C,D,E
将被由于%>%
运算符,作为隐式第一个参数传递的data.frame中的字段。
简而言之,您需要调用 myCustomFunction(A,B,C,D,E ...)
,使参数的范围正确,例如:
data_整理< - data%>%
<某些其他功能,如mutuate,filter等清理。
brind_rows(do.call(myCustomFunction,data_tidy))
Using DPLYR and TIDYR, I'm trying to create a tidy version of a dataset where rows can be missing depending on the data of certain columns. I created a function that returns the rows missing (by creating them with default data) in a new tbl_df(data.frame) (I unit-tested it and it works okay with specific data).
However, when calling it from 'bind_rows', I get the following error: Error in data.frame(a, b, c,...: Object 'A' not found.
For example, my data looks like this:
A B C D E ...
a1 b1 c1 d1 e1 ...
a2 b2 c2 d2 e2 ...
...
My code looks like this:
data_tidy <- data %>%
<some other functions to clean up like 'mutuate', 'filter', etc.> %>%
brind_rows(myCustomFunction(A, B, C, D, E... ))
Any ideas what I'm doing wrong? I'm still new to R, DPLYR/TIDYR...
Note: If I remove the last call to 'bind_rows', the table is cleanup as expected with the proper A, B, C, etc. columns. I also use a 'for' loop in this specific scenario which I know might not be optimal but for now, I will work with this version so I can get it to work and then try to optimize my code (or vectorize).
Thanks!
In your call to foo %>% brind_rows(myCustomFunction(A, B, C, D, E... ))
, myCustomFunction(A, B, C, D, E... )
is being called as an ordinary R function, whereas I think you'r expecting that it be evaluted within the context of a dplyr function as in mutate(x = myCustomFunction(A, B, C, D, E... ))
where the arguments A, B, C, D, E
would be replaced by fields from the data.frame that is passed as the implicit first argument thanks to the %>%
operator.
In short, you need to call myCustomFunction(A, B, C, D, E... )
in such a way that the arguments are scoped correctly, such as:
data_tidy <- data %>%
<some other functions to clean up like 'mutuate', 'filter', etc.>
brind_rows(do.call(myCustomFunction,data_tidy))
这篇关于R:DPLYR包:当调用自定义函数时,bind_rows失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!