I have a multiple lists in my environment(all start with "CDS_"). Each list is conducted of multiple sub lists.I want to call the lists one by one to apply a function for each of these objects. This is what I am trying:
lists<-grep("CDS_",names(.GlobalEnv),value=TRUE) #Lists all objectrs staring with "CDS_" for (i in seq_along(lists)){ data<-do.call("list",mget(lists[i])) #this line blends all sub lists into one list assign(paste("Df_", lists[i], sep = "_"), my_function(data) # my_function requires a list with multiple sub lists } but the issue is the do.call("list",mget(lists[i])) blends all sub lists into one. For example if there is a list with one sub list it returns the list but all sub lists go into one!
Any solutions how to make this work?
here is a sample to test:
#Defining my_function pulling out the sub list which contains "sample1" my_function<-function(.data){ # pull out the undergraduate data grep("sample1", .data, value = TRUE) } # 1st list list_1 <- list(1:54, c("This","is","sample1","for","list1"), c("This","is","sample2","for","list1"), "Hi") # 2nd list list_2 <- list(51:120, c("This","is","sample1","for","list1"), c("This","is","sample2","for","list1"), "Bus") # 3rd list list_3 <- list(90:120, letters[16:11], 2025) lists<-grep("list_",names(.GlobalEnv),value=TRUE) for (i in seq_along(lists)){ data<-do.call("list",mget(lists[i])) assign(paste("sample1_", lists[i], sep = ""), my_function(data)) }
do.callhere. You seem to be callingmget()with only value value at a time. Messing withmgetandassignis often a messy alternative to just sticking with a list.