-5

I want to access a table, but the name of the table is stored in a variable.

How do i use this variable to read the table?

Example :

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) 

How do i access the data table with the name stored in 'TABLE_NAME' ?

Anup
  • 1
  • 1

1 Answers1

0

While I would recommend you work in a list, you can always use

> 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

To use a list, you would do

> 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
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197