Reputation: 77
I need to create a lot of dataframes. is there any faster way where you can only specify the names of new tables and assign <- data.frame () once ?
results_test <- data.frame()
results_OA<- data.frame()
results_SVM <- data.frame()
results_SVM_linear <- data.frame()
results_SVM_RBF <- data.frame()
Upvotes: 2
Views: 45
Reputation: 679
Here is using for loop, base R
names<-c("test", "OA", "SVM", "SVM_linear", "SVM_RBF")
for(i in 1:length(names)){
assign(paste0("results_",names[i]),data.frame())
}
Upvotes: 1
Reputation: 72778
Use replicate
to create a list of n
data frames, name them using setNames
. Finally, with list2env
put the list objects into e.g. the global environment.
df.names <- paste0("results_", c("test", "OA", "SVM", "SVM_linear", "SVM_RBF"))
df.list <- setNames(replicate(n=length(df.names)), df.names)
list2env(df.list, envir=.GlobalEnv)
Result:
ls()
# [1] "df.list" "results_OA" "results_SVM" "results_SVM_linear"
# [5] "results_SVM_RBF" "results_test"
Upvotes: 3