I'm having a very simple problem, but have not found a way out of it. I was given about 200 tables from different stores, and have to analyze the whole body of data as a unit. As the tables are fairly big, I'm working with data.table::fread. My plan is loading each table, rbinding them together and proceed with the analysis. In order to retain which store is which, I'd like to add a variable store to each table, with the name of the store(same as the table). For that I planned to run a for loop with the names of the tables and create the new variable.
# I'll create some dummy data tables here. In reality they're loaded with fread
library(data.table)
centro <- data.table(x=letters[1:5], y = 1:5)
sur <- data.table(x=letters[2:7], y = 3:8)
...
norte <- data.table(x=letters[2:10], y = 3:11)
I need each table to have a variable "store" whose value is "centro", "sur" or "norte", depending on the store it belongs to. So I tried:
st <- c("centro", "sur", "norte")
for (i in st){
i[, store := i]
}
Which, of course, didn't work (it complains that "i is not a data.table"). I then tried to create a list and try it via lapply:
sts <- list(centro, sur, norte)
sts <- lapply(sts, function(z) z[, store := names(z)])
Which doesn't work because names(z) are "x", "y" and "store".
How can I do this with a loop (or a *pply function), instead of manually doing xyz[, store := "xyz"] ?
side notes
- the naming of the variables is almost impossible to adjust, and doesn't follow any pattern (nothing like store_1, store_2, etc)
- I may be very biased with the alternative I described. Any other thing that works will be ok.
i[, store := i]is not reproducible,iis a character scalar, and you are subsetting it with[and trying to use:=inside. Start from there.