R: DPLYR package: bind_rows failing when calls a custom function

会有一股神秘感。 提交于 2019-12-12 04:25:49

问题


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!


回答1:


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))


来源:https://stackoverflow.com/questions/28752976/r-dplyr-package-bind-rows-failing-when-calls-a-custom-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!