这应该是一个简单的,我希望。我有几个数据框加载到工作区,标记为 df01 到 df100,并非所有数字都表示。我想在所有数据集中绘制特定列,例如在箱形图中。如何使用通配符引用以 df 开头的所有对象,即:
boxplot(df00$col1, df02$col1, df04$col1)
=
boxplot(df*$col1)
惯用的方法是使用列表,或使用单独的环境。
ls
您可以使用和创建此列表pattern
df.names <- ls(pattern = '^df')
# note
# ls(pattern ='^df[[:digit:]]{2,}')
# may be safer if there are objects starting with df you don't want
df.list <- mget(df.names)
# note if you are using a version of R prior to R 3.0.0
# you will need `envir = parent.frame()`
# mget(ls(pattern = 'df'), envir = parent.frame())
# use `lapply` to extract the relevant columns
df.col1 <- lapply(df.list, '[[', 'col1')
# call boxplot
boxplot(df.col1)
尝试这个:
nums <- sprintf("%02d", 0:100)
dfs.names <- Filter(exists, paste0("df", nums))
dfs.obj <- lapply(dfs.names, get)
dfs.col1 <- lapply(dfs.obj, `[[`, "col1")
do.call(boxplot, dfs.col1)