我有一个2005年至2016年的数据框架列表。除了年份的数字外,它们的编写方式相同:
m =list(X2016_kvish_1_10t = X2016_kvish_1_10t, X2015_kvish_1_10t = X2015_kvish_1_10t, X2014_kvish_1_10t = X2014_kvish_1_10t,
X2013_kvish_1_10t = X2013_kvish_1_10t, X2012_kvish_1_10t = X2012_kvish_1_10t, X2011_kvish_1_10t = X2011_kvish_1_10t,
X2010_kvish_1_10t = X2010_kvish_1_10t, X2009_kvish_1_10t = X2009_kvish_1_10t, X2008_kvish_1_10t = X2008_kvish_1_10t,
X2007_kvish_1_10t = X2007_kvish_1_10t, X2006_kvish_1_10t = X2006_kvish_1_10t, X2005_kvish_1_10t = X2005_kvish_1_10t)
是否有更短的编写方式,而无需单独编写所有内容?
答案 0 :(得分:3)
尝试mget
:
df_names = paste0("X", 2005:2016, "_kvish_1_10t")
m = mget(df_names)
修改强>
正如@ d.b指出的那样,你甚至不需要创建df_names
m = mget(ls(pattern="_kvish_1_10t$"))
答案 1 :(得分:0)
您可以使用mget
函数在工作区中提供对象名称的字符向量。
我制作了一个可复制的例子,目的是展示如何做到这一点。
df_name <- paste0("x", 2005:2016, "_kvish_1_10t")
df_name
#> [1] "x2005_kvish_1_10t" "x2006_kvish_1_10t" "x2007_kvish_1_10t"
#> [4] "x2008_kvish_1_10t" "x2009_kvish_1_10t" "x2010_kvish_1_10t"
#> [7] "x2011_kvish_1_10t" "x2012_kvish_1_10t" "x2013_kvish_1_10t"
#> [10] "x2014_kvish_1_10t" "x2015_kvish_1_10t" "x2016_kvish_1_10t"
# juste create some dummy table for example
l <- lapply(df_name, assign, value = mtcars[1:2], envir= .GlobalEnv)
# Use mget to get a list of all the object
m <- mget(df_name, envir = .GlobalEnv)
str(m)
#> List of 12
#> $ x2005_kvish_1_10t:'data.frame': 32 obs. of 2 variables:
#> ..$ mpg: num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#> ..$ cyl: num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
#> $ x2006_kvish_1_10t:'data.frame': 32 obs. of 2 variables:
#> ..$ mpg: num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#> ..$ cyl: num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
#> $ x2007_kvish_1_10t:'data.frame': 32 obs. of 2 variables:
#> ..$ mpg: num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#> ..$ cyl: num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
#> $ x2008_kvish_1_10t:'data.frame': 32 obs. of 2 variables:
#> ..$ mpg: num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#> ..$ cyl: num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
#> $ x2009_kvish_1_10t:'data.frame': 32 obs. of 2 variables:
#> ..$ mpg: num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#> ..$ cyl: num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
#> $ x2010_kvish_1_10t:'data.frame': 32 obs. of 2 variables:
#> ..$ mpg: num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#> ..$ cyl: num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
#> $ x2011_kvish_1_10t:'data.frame': 32 obs. of 2 variables:
#> ..$ mpg: num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#> ..$ cyl: num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
#> $ x2012_kvish_1_10t:'data.frame': 32 obs. of 2 variables:
#> ..$ mpg: num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#> ..$ cyl: num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
#> $ x2013_kvish_1_10t:'data.frame': 32 obs. of 2 variables:
#> ..$ mpg: num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#> ..$ cyl: num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
#> $ x2014_kvish_1_10t:'data.frame': 32 obs. of 2 variables:
#> ..$ mpg: num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#> ..$ cyl: num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
#> $ x2015_kvish_1_10t:'data.frame': 32 obs. of 2 variables:
#> ..$ mpg: num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#> ..$ cyl: num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
#> $ x2016_kvish_1_10t:'data.frame': 32 obs. of 2 variables:
#> ..$ mpg: num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#> ..$ cyl: num [1:32] 6 6 4 6 8 6 8 4 4 6 ...