Reputation: 53
I am trying to implement the following:
dataset_id_1 = subset(data, id == 1)
dataset_id_2 = subset(data, id == 2)
dataset_id_3 = subset(data, id == 3)
However, I need to do this for more than 100 IDs. I encounter a problem in generating the name of the dataset on the left. I tried the following:
for (i in 1:120) {
dataset_id_[[i]] = subset(data, id == i)
}
Do you know how to save the name of the dataset according to the specified id?
Thank you so much
Upvotes: 0
Views: 634
Reputation: 2485
Try this
for (i in 1:120) {
assign(paste("dataset_id_", i), subset(data, id == i) )
}
Upvotes: 0
Reputation: 39595
Try this:
#List
List <- list()
#Loop
for (i in 1:120) {
List[[i]] = subset(data, id == i)
}
#Names
names(List) <- paste0('dataset_id_',1:length(List))
#Set to envir
list2env(List,envir = .GlobalEnv)
Upvotes: 0
Reputation: 101335
Try split
+ list2env
like below
lst <- split(volping, volping$id)
list2env(setNames(lst,paste0("dataset_id_",names(lst))), .GlobalEnv)
Upvotes: 0