[英]Calling variable in user-defined function with reshape2::melt and reshape2::dcast
I would like to convert this data frame 我想转换这个数据框
data <- data.frame(color=c("red","red","red","green","green","green","blue","blue","blue"),object=c("box","chair","table","box","chair","table","box","chair","table"),units=c(1:9),price=c(11.5,12.5,13.5,14.5,15.5,16.5,17.5,18.5,19.5))
to this other one 到另一个
output <- data.frame(color=c("red","green","blue"),units_box=c(1,4,7),price_box=c(11.5,14.5,17.5), units_chair=c(2,5,8),price_chair=c(12.5,15.5,18.5),units_table=c(3,6,9),price_table=c(13.5,16.5,19.5))
Therefore, I am using reshape2::melt
and reshape2::dcast
to build a user-defined function as the following 因此,我使用
reshape2::melt
和reshape2::dcast
来构建用户定义的函数,如下所示
fun<-function(df,var,group){
r<-reshape2::melt(df,id.vars=var)
r<-reshape2::dcast(r,var~group)
return(r)
}
When I use the function as follows 当我如下使用函数时
fun(data,color,object) 有趣的(数据,颜色,对象)
I get the following error message 我收到以下错误消息
Error in melt_check(data, id.vars, measure.vars, variable.name, value.name) : object 'color' not found
melt_check(数据,id.vars,measure.vars,variable.name,value.name)中的错误:找不到对象'颜色'
Do you know how can I solve it? 你知道我该怎么解决吗? I think that the problem is that I should call the variables in
reshape2::melt
with quotes but I do not know how. 我认为问题是我应该在
reshape2::melt
中用引号调用变量,但我不知道如何。
Note 1: I would like keep the original number format of variables (ie objects without decimals and price with one decimal) 注意1:我想保留变量的原始数字格式(即不带小数的对象和带小数的价格)
Note 2: I would like to remark that that my real code (this is just a simplified example) is much longer and involves dplyr
functions (including enquo() and UQ() functions). 注2:我想指出,我的实际代码(这只是一个简化的示例)要长得多,并且涉及
dplyr
函数(包括enquo()和UQ()函数)。 Therefore the solutions for this case should be compatible with dplyr
. 因此,这种情况下的解决方案应与
dplyr
兼容。
Note 3: I do not use tidyr (I am a big fun of the whole tidyverse) because the current tidyr
still use the old language for functions and I share the script with other people that might not be willing to use the development version of tidyr
. 注3:我不使用tidyr(我对整个tidyverse感到很开心),因为当前的
tidyr
仍然使用旧语言编写函数,并且与其他不愿意使用tidyr
开发版本的人共享脚本。 。
We can use dcast
from data.table
我们可以使用
dcast
的data.table
library(data.table)
dcast(setDT(data), color ~object, value.var = c("units", "price"), FUN = c(length, mean))
I solved the issue by myself (although I do not know very well the reasons behind). 我自己解决了这个问题(尽管我不太清楚背后的原因)。 The main problem, as I suspected was passing the variables of the user-defined function in melt and dcast cause some kind of conflict maybe due to the lack of quotes (?).
我怀疑主要的问题是在melt和dcast中传递用户定义函数的变量,这可能是由于缺少引号(?)导致某种冲突。 Anyway I renamed the variables using dplyr::rename so that the names are not anymore depended of variables but characters.
无论如何,我使用dplyr :: rename重命名了变量,以便名称不再取决于变量,而取决于字符。 Here you can see the final code I am applying:
在这里,您可以看到我正在应用的最终代码:
fun<-function(df,var,group){
enquo_var<-enquo(var)
enquo_group<-enquo(group)
r<-df%>%
reshape2::melt(., id.var=1, variable.name = "parameter")%>%
dplyr::rename(var = UQ(enquo_var))%>%
reshape2::dcast(data=., formula = var~parameter, value.var = "value")
return(r)
}
funx<-fun(data,color,object)
Although I found the solution to my particular problem, I would appreciate very much if someone explains me the reasons behind. 尽管我找到了解决自己特定问题的方法,但如果有人向我解释其背后的原因,我将不胜感激。
PS: I hope anyway that the new version of tidyr is ready soon to make such tasks easier. PS:无论如何,我希望新版本的Tidyr可以使这些任务变得更容易。 Thanks @hadley for your fantastic work.
感谢@hadley所做的出色工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.