如何访问我的表名是变量的表

时间:2019-06-01 07:29:42

标签: r

我想访问一个表,但是表的名称存储在一个变量中。

如何使用此变量读取表?

示例:

DATA1 <- data.frame(SR = c(1:10), VALUE = seq(1,100,10))   #### has 10 rows 
DATA2<- data.frame(SR = c(1:11), VALUE = seq(1,110,10))  #### has 11 rows 
DATA3<- data.frame(SR = c(1:12), VALUE = seq(1,120,10))  #### has 12 rows 

##### ---- list of data table 
DATA_TABLE_LIST <- list(DATA1,DATA2,DATA3)

#### ----- given names to elements of list
names(DATA_TABLE_LIST) <- c("DATA1","DATA2","DATA3"  )

#### ---- identify element of list which has minimum rows 
TABLE_NAME <- names(DATA_TABLE_LIST)[which(  sapply(DATA_TABLE_LIST,nrow) == min(sapply(DATA_TABLE_LIST,nrow)))]

####   ---- 'TABLE_NAME' stores the name of a datatable which has the minimum rows ( in this case it is DATA1) 

如何使用存储在“ TABLE_NAME”中的名称访问数据表?

1 个答案:

答案 0 :(得分:0)

虽然我建议您使用列表,但您始终可以使用

> get(TABLE_NAME)
   SR VALUE
1   1     1
2   2    11
3   3    21
4   4    31
5   5    41
6   6    51
7   7    61
8   8    71
9   9    81
10 10    91

要使用列表,您可以

> DATA_TABLE_LIST[TABLE_NAME]
$DATA1
   SR VALUE
1   1     1
2   2    11
3   3    21
4   4    31
5   5    41
6   6    51
7   7    61
8   8    71
9   9    81
10 10    91