Reputation: 684
I like to create separate variables in R which each read different tables as below:
AA_1_11 <- read.table(File_AA_1_11.dta,header=TRUE))
AA_2_22 <- read.table(File_AA_2_22.dta,header=TRUE))
AA_3_33 <- read.table(File_AA_3_33.dta,header=TRUE))
AA_4_44 <- read.table(File_AA_4_44.dta,header=TRUE))
BB_1_11 <- read.table(File_BB_1_11.dta,header=TRUE))
BB_2_22 <- read.table(File_BB_2_22.dta,header=TRUE))
BB_3_33 <- read.table(File_BB_3_33.dta,header=TRUE))
BB_4_44 <- read.table(File_BB_4_44.dta,header=TRUE))
Through here, I have managed to create separate variables and assign a numerical value, x to each. The x could be easily replaced with an equation of a function of x, f(x),
char_list <- c("AA", "BB")
num_list <- c("1_11", "2_22", "3_33", "4_44")
x <- 1
for (char in char_list) {
for (num in num_list) {
assign(paste(char,num, sep = '_'), x)
x <- x + 1
}
}
The same approach seems does not work for the below. It is not executing reading a table.
char_list <- c("AA", "BB")
num_list <- c("1_11", "2_22", "3_33", "4_44")
for (char in char_list) {
for (num in num_list) {
temp <- paste('"D:/File_',ant,'_',sta,'.dta"',sep = '')
assign(paste(char,num, sep = '_'), read.table(temp,header=TRUE))
}
}
From HELP library - The assign(x, value) syntax not sure if value only applies to numbers.
Can you please guide if there is other way to create separate variables and execute to read different read tables by each of the variable?
Update: List Experimented suggestion by @Roland. Did not quite get if this was what suggested.
char_list <- c("AA", "BB")
num_list <- c("1_11", "2_22", "3_33", "4_44")
char_num_list <- list()
for (char in char_list) {
char_num_list[[char]] <- list()
for (num in num_list) {
char_num_list[[char]][[num]] <- paste('"D:/File_',char,'_',num,'.dta"',sep = '')
assign(paste(char,num, sep = '_'), read.table(char_num_list[[char]][[num]],header=TRUE))
}
}
Upvotes: 1
Views: 83
Reputation: 37879
When you are using a list there is no need to use assign
as well. The whole idea of putting everything in a list is to avoid setting variables with assign
.
Example data.frame since I cannot read your files:
a <- data.frame(a=runif(50))
Solution
char_list <- c("AA", "BB")
num_list <- c("1_11", "2_22", "3_33", "4_44")
char_num_list <- list()
for (char in char_list) {
char_num_list[[char]] <- list()
for (num in num_list) {
temp <- paste('"D:/File_',char,'_',num,'.dta"',sep = '') #save the path for each file
char_num_list[[char]][[num]] <- a #I am using a here but you should be using your read.table
}
}
#replace a with read.table(temp,header=TRUE) for your tables
Output:
> str(char_num_list)
List of 2
$ AA:List of 4
..$ 1_11:'data.frame': 50 obs. of 1 variable:
.. ..$ a: num [1:50] 0.24 0.383 0.89 0.675 0.736 ...
..$ 2_22:'data.frame': 50 obs. of 1 variable:
.. ..$ a: num [1:50] 0.24 0.383 0.89 0.675 0.736 ...
..$ 3_33:'data.frame': 50 obs. of 1 variable:
.. ..$ a: num [1:50] 0.24 0.383 0.89 0.675 0.736 ...
..$ 4_44:'data.frame': 50 obs. of 1 variable:
.. ..$ a: num [1:50] 0.24 0.383 0.89 0.675 0.736 ...
$ BB:List of 4
..$ 1_11:'data.frame': 50 obs. of 1 variable:
.. ..$ a: num [1:50] 0.24 0.383 0.89 0.675 0.736 ...
..$ 2_22:'data.frame': 50 obs. of 1 variable:
.. ..$ a: num [1:50] 0.24 0.383 0.89 0.675 0.736 ...
..$ 3_33:'data.frame': 50 obs. of 1 variable:
.. ..$ a: num [1:50] 0.24 0.383 0.89 0.675 0.736 ...
..$ 4_44:'data.frame': 50 obs. of 1 variable:
.. ..$ a: num [1:50] 0.24 0.383 0.89 0.675 0.736 ...
As you can see by using a list you do not use assign at all, which will make your life easier anyway.
Upvotes: 2