Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在“聚合”函数中包装“因子级别名称”?_R - Fatal编程技术网

R 如何在“聚合”函数中包装“因子级别名称”?

R 如何在“聚合”函数中包装“因子级别名称”?,r,R,我计划在脚本中使用上述名称作为xlab,如下所示: > levels(iris$Species) [1] "setosa" "versicolor" "virginica" 在qqPlot之后,我发现xlab未解析 一般问题是: 如何在aggregate函数中包装factor-level-name,如aggregate(y~因子,df,函数(x){factor-level-name})?这里有两种方法,我相信这个问题需要第二种方法。这两个都不需要聚合 1.第一种方法是直接使用

我计划在脚本中使用上述名称作为
xlab
,如下所示:

> levels(iris$Species)
[1] "setosa"     "versicolor" "virginica" 

qqPlot
之后,我发现
xlab
未解析

一般问题是:

如何在
aggregate
函数中包装
factor-level-name
,如
aggregate(y~因子,df,函数(x){factor-level-name})

这里有两种方法,我相信这个问题需要第二种方法。这两个都不需要聚合

1.第一种方法是直接使用公式界面。它将所有图形放在同一页/图中

aggregate(Sepal.Length~Species,iris,function(x){
        car::qqPlot(x,distribution="norm",xlab="names(x)")
})
2.第二种方法是按因子的级别拆分数据集,然后
lappy
是一个匿名函数,用于分别绘制每个QQ图

car::qqPlot(Sepal.Length ~ Species, data = iris, distribution = "norm")

sp
car::qqPlot(Sepal.Length~Species,data=iris,distribution=“norm”)
把所有的东西都放在同一个2x2网格图中。在
聚合(y~因子,df,函数(x){do something to x})
中,
x
有名字吗?其他类似的命令,如
taaply
groupby
,有人可以包装级别名称吗?@kittygirl
x
没有名称,您会对因子的名称感兴趣,而不是
y
,对吗?请注意,在我的第二个代码示例中,您可以跳过
sp
的创建,并将
拆分(.)
放在
Sepal.Length~Species
中。当将
Sepal.Length~Species
放入
qqPlot
中时,没有输出值。使用
聚合
时,有一些有价值的输出。我不知道这些输出是否有价值?@kittygirl你说没有输出是什么意思?我的第二个解决方案绘制了所有3个QQ图,每个级别的
物种一个,级别为x轴标题。您正在运行的代码是什么?
sp <- split(iris, iris$Species)
lapply(seq_along(sp), function(i){
  sp[[i]]$Species <- droplevels(sp[[i]]$Species)
  car::qqPlot(Sepal.Length ~ Species, data = sp[[i]], 
              distribution = "norm", xlab = names(sp)[i])
})
rm(sp)    # final clean up