Yes, I know there are lots of questions about dynamically naming variables in R, and yes, I have reviewed them, and no, I still can't do what I want to do.
是的,我知道在R中有很多关于动态命名变量的问题,是的,我已经审查了它们,不,我仍然不能做我想做的事情。
I have created five different data frames, each holding selected info from another data frame. I want to send each of these smaller data frames to ggplot. I've created the frames, but I can't seem to get ggplot to resolve the dynamic data frame name. I've tried various version of assign, and as.name.
我创建了五个不同的数据帧,每个数据帧保存来自另一个数据帧的选定信息。我想将每个较小的数据帧发送到ggplot。我已经创建了框架,但我似乎无法获得ggplot来解析动态数据框架名称。我尝试了各种版本的assign和as.name。
Here's my truncated code:
这是我的截断代码:
Alist <- unique(bigdf$initial.A)
Blist <- unique(bigdf$initial.B)
for ( i in Alist ) {
for ( j in Blist ) {
assign( paste0("a", i, "b", j ), bigdf %>%
filter( initial.A == i, initial.B == j ))
}
}
This works just fine, I have created the correct number of data frames, correctly named, and containing the correct info: a5b10, a5b20, a5b30, a10b10, a10b20 and a10b30.
这很好用,我创建了正确数量的数据帧,正确命名,并包含正确的信息:a5b10,a5b20,a5b30,a10b10,a10b20和a10b30。
for (i in Alist) {
for (j in Blist ) {
png(paste0("a", i, "b", j, ".png"))
useframe <- as.name(paste0("a", i, "b", j))
ggplot(useframe, aes(step)) + . . .
}
}
Errors are usually along the lines of: ggplot2 doesn't know how to deal with data of class character ( or of class name ).
错误通常与以下几行有关:ggplot2不知道如何处理类字符(或类名)的数据。
How do I create a dynamic name and assign it the contents of the matching data.frame?
如何创建动态名称并为其分配匹配的data.frame的内容?
And if you'd like me to use lists, I'll need to better understand how that would apply here.
如果您希望我使用列表,我需要更好地了解这将如何应用。
2 个解决方案
#1
2
get
should solve your problem:
得到应该解决你的问题:
for (i in Alist){
for (j in Blist){
png(paste0("a", i, "b", j, ".png"))
useframe <- paste0("a", i, "b", j)
ggplot(get(useframe), aes(x = step)) + geom_density()
}
}
But you can easily do away with all that mess and use doParallel
with a binary search on a data.table
:
但你可以轻松地消除所有混乱,并使用doParallel和data.table上的二进制搜索:
library(doParallel)
library(data.table)
bigDT <- setDT(bigdf)
setkeyv(bigDT, c("intial.A", "initial.B"))
foreach(i = seq(Alist)) %do% {
foreach(j = seq(Blist)) %do% {
x <- bigDT[J(c(Alist[i], Blist[i]))]
png(paste0("a", i, "b", j, ".png"))
ggplot(x, aes(step)) + . . .
}
}
#2
0
Why can't you just keep the data all together and use a facet grid?
为什么不能将数据全部保存在一起并使用构面网格?
EDIT:
编辑:
library(ggplot2)
library(dplyr)
library(magrittr)
plot_data = function(sub_df) {
filename =
sub_df %>%
use_series(gear) %>%
first %>%
sprintf("gear_%s.pdf", .)
(ggplot(sub_df) +
aes(x = wt, y = mpg) +
geom_point() ) %>%
ggsave(filename, .)
data_frame()
}
mtcars %>%
group_by(gear) %>%
do(plot_data(.) )
#1
2
get
should solve your problem:
得到应该解决你的问题:
for (i in Alist){
for (j in Blist){
png(paste0("a", i, "b", j, ".png"))
useframe <- paste0("a", i, "b", j)
ggplot(get(useframe), aes(x = step)) + geom_density()
}
}
But you can easily do away with all that mess and use doParallel
with a binary search on a data.table
:
但你可以轻松地消除所有混乱,并使用doParallel和data.table上的二进制搜索:
library(doParallel)
library(data.table)
bigDT <- setDT(bigdf)
setkeyv(bigDT, c("intial.A", "initial.B"))
foreach(i = seq(Alist)) %do% {
foreach(j = seq(Blist)) %do% {
x <- bigDT[J(c(Alist[i], Blist[i]))]
png(paste0("a", i, "b", j, ".png"))
ggplot(x, aes(step)) + . . .
}
}
#2
0
Why can't you just keep the data all together and use a facet grid?
为什么不能将数据全部保存在一起并使用构面网格?
EDIT:
编辑:
library(ggplot2)
library(dplyr)
library(magrittr)
plot_data = function(sub_df) {
filename =
sub_df %>%
use_series(gear) %>%
first %>%
sprintf("gear_%s.pdf", .)
(ggplot(sub_df) +
aes(x = wt, y = mpg) +
geom_point() ) %>%
ggsave(filename, .)
data_frame()
}
mtcars %>%
group_by(gear) %>%
do(plot_data(.) )