英文:
"Error in as.double(x)..." and other error message while creating a function to summarize and graph
问题
以下是翻译好的部分:
"long time listener and first time caller here"
长期的倾听者,第一次打电话来这里。
"For a project I am working on, I often end up graphing the same graphs with just different response variables. So I am trying to write a function based on a ddply()
code and a ggplot()
code I keep reusing:"
对于我正在进行的项目,我经常需要绘制相同的图表,只是响应变量不同。因此,我尝试编写一个基于我反复使用的 ddply()
代码和 ggplot()
代码的函数:
"(df.smpl is the dataframe I am working with, genotype is the treatment I am interested in, and var is the stand-in for a response variable I am interested in)"
(df.smpl 是我正在使用的数据框,genotype 是我感兴趣的处理方法,var 是我感兴趣的响应变量的替代符号)
"const.gra<-function(var){"
const.gra<-function(var){
"## First, summarise the data to be used in subsequent ggplot code"
首先,总结数据以在后续的 ggplot 代码中使用
"summ<-ddply(df.smpl, "genotype", summarise,"
summ<-ddply(df.smpl, "genotype", summarise,
"N = length(var),"
"N = length(var),"
"mean = mean(var),"
"mean = mean(var),"
"sd = sd(var),"
"sd = sd(var),"
"se = sd/sqrt(N))"
"se = sd/sqrt(N))"
"# Now graph"
现在绘制图表
"ggplot(data=summ, aes(genotype, mean))+"
ggplot(data=summ, aes(genotype, mean))+
"geom_col(position = "dodge")+"
geom_col(position = "dodge")+
"geom_errorbar(aes(ymin=mean-se, ymax=mean+se),"
geom_errorbar(aes(ymin=mean-se, ymax=mean+se),
"width=.2,"
"width=.2,"
"position=position_dodge(.9))+"
"position=position_dodge(.9))+"
"scale_x_discrete(name = "Genotype","
"scale_x_discrete(name = "Genotype","
"breaks=c("K","PW", "AW"),"
"breaks=c('K','PW', 'AW'),"
"labels=c("Plant K", "Plant PW", "Plant AW"))+"
"labels=c('植物K', '植物PW', '植物AW'))+"
"scale_y_continuous(name = "Title")+"
"scale_y_continuous(name = '标题')+"
"theme(legend.position = "none", "
"theme(legend.position = 'none', "
"legend.justification = c(1,1),"
"legend.justification = c(1,1),"
"panel.background = element_rect(fill = "white"),"
"panel.background = element_rect(fill = 'white'),"
"legend.key = element_rect(fill = "white"),"
"legend.key = element_rect(fill = 'white'),"
"axis.line = element_line(colour = "black"),"
"axis.line = element_line(colour = 'black'),"
"axis.ticks.x = element_blank(),"
"axis.ticks.x = element_blank(),"
"axis.text = element_text(size = 14),"
"axis.text = element_text(size = 14),"
"axis.title = element_text(size = 14),"
"axis.title = element_text(size = 14),"
"legend.text = element_text (size = 14),"
"legend.text = element_text(size = 14),"
"legend.title = element_text (size = 14))"
"legend.title = element_text(size = 14))"
"const.gra(df.smpl$bgbm..mg.)"
const.gra(df.smpl$bgbm..mg.)"
"But the above codes yield the following error messages."
但是上述代码产生了以下错误消息。
"> "> Error in as.double(x) : "> cannot coerce type 'closure' to vector of type 'double' "> In addition: Warning message: "> In mean.default(var) : argument is not numeric or logical: returning NA ">
"
"> Tried solving it on my own but have been very unsuccessful so far. The codes run just fine if I were to run them verbatim outside of the function."
我尝试自己解决问题,但到目前为止一直不太成功。如果我在函数之外逐字运行这些代码,它们就可以正常运行。
英文:
long time listener and first time caller here
For a project I am working on, I often end up graphing the same graphs with just different response variables. So I am trying to write a function based on a ddply()
code and a ggplot()
code I keep reusing:
(df.smpl is the dataframe I am working with, genotype is the treatment I am interested in, and var is the stand-in for a response variable I am interested in)
const.gra<-function(var){
## First, summarise the data to be used in subsequent ggplot code
summ<-ddply(df.smpl, "genotype", summarise,
N = length(var),
mean = mean(var),
sd = sd(var),
se = sd/sqrt(N))
# Now graph
ggplot(data=summ, aes(genotype, mean))+
geom_col(position = "dodge")+
geom_errorbar(aes(ymin=mean-se, ymax=mean+se),
width=.2,
position=position_dodge(.9))+
scale_x_discrete(name = "Genotype",
breaks=c("K","PW", "AW"),
labels=c("Plant K", "Plant PW", "Plant AW"))+
scale_y_continuous(name = "Title")+
theme(legend.position = "none",
legend.justification = c(1,1),
panel.background = element_rect(fill = "white"),
legend.key = element_rect(fill = "white"),
axis.line = element_line(colour = "black"),
axis.ticks.x = element_blank(),
axis.text = element_text(size = 14),
axis.title = element_text(size = 14),
legend.text = element_text (size = 14),
legend.title = element_text (size = 14))
}
const.gra(df.smpl$bgbm..mg.)
But the above codes yield the following error messages.
>
> Error in as.double(x) :
> cannot coerce type 'closure' to vector of type 'double'
> In addition: Warning message:
> In mean.default(var) : argument is not numeric or logical: returning NA
>
Tried solving it on my own but have been very unsuccessful so far. The codes run just fine if I were to run them verbatim outside of the function.
Based on some answers I have found online re: the error code, I tried subbing out some strings that sounded like they could be common base r function names or something, but no luck thus far...
答案1
得分: 1
首先,错误消息是由于 sd(var)
和 mean(var)
。在 plyr::summarise
调用的某个时刻,R 会在你的数据框中查找名为 var
的列,当没有找到时,它会在调用 const.gra
的父环境中查找。在那里,它会找到默认加载的 stats
包中的 var
函数,然后将其传递给不接受其他函数作为参数的函数。
第二点要注意的是,plyr
包已经不再更新,开发者的存储库建议使用 dplyr
替代。
根据我现在进行的一些快速实验,我认为 plyr
不支持 tidyverse
包中可用的当前非标准评估语法。幸运的是,两者之间似乎有足够的兼容性,你可以在 plyr::ddply
调用内部使用 dplyr::summarise
,而不需要改变太多代码。
也就是说,我建议你完全放弃 plyr
。下面你可以找到两种做事情的方式。请注意,如果你先加载 dplyr
然后再加载 plyr
,那么前者的 summarise
将被后者掩盖。
library(plyr)
library(dplyr)
func_nse <- function(y, x) {
ddply(y, "vs", summarise,
N = length({{x}}),
mean = mean({{x}}),
sd = sd({{x}}),
se = sd/sqrt(N))
}
func_dplyr <- function(y, x) {
y %>%
group_by(vs) %>%
summarise(N = length({{x}}),
mean = mean({{x}}),
sd = sd({{x}}),
se = sd/sqrt(N))
}
func_nse(mtcars, mpg)
func_dplyr(mtcars, mpg)
英文:
There are a few things to unpack here.
First, the error messages are due to sd(var)
and mean(var)
. At some point in the plyr::summarise
call, R looks for a column called var
in your data frame, and after not finding one, it looks in the parent environment from where you're calling const.gra
. There it finds the var
function in the stats
package that is loaded by default in R, and then passes it to functions that don't like other functions as their argument.
The second thing to note is that the plyr
package is retired and the developer's repo recommends dplyr
be used instead.
Based on some quick experiments I did now, I don't think plyr
supports the current non-standard evaluation syntax that is available in tidyverse
packages. Luckily, there seems to be enough compatibility between both, that you can use dplyr::summarise
inside the plyr::ddply
call and things will work without changing too much code.
That said, I would advise you drop plyr
completely. Below you can find both ways of doing things. Be aware that if you load dplyr
first and then plyr
, then the former's summarise
will be masked by the latter.
library(plyr)
library(dplyr)
func_nse <- function(y, x) {
ddply(y, "vs", summarise,
N = length({{x}}),
mean = mean({{x}}),
sd = sd({{x}}),
se = sd/sqrt(N))
}
func_dplyr <- function(y, x) {
y %>%
group_by(vs) %>%
summarise(N = length({{x}}),
mean = mean({{x}}),
sd = sd({{x}}),
se = sd/sqrt(N))
}
func_nse(mtcars, mpg)
func_dplyr(mtcars, mpg)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论