Create many empty dataframes at one time

Hi,how to create many empty dataframes at one time? I try, it seems that doesn't work :upside_down_face:
Appreciate any help!

nam=c("c1","c2","c3")
for(z in 1:3){
  nam[z]<-data.frame(matrix(NA, nrow = 3, ncol = 2)
}

Created on 2023-06-01 with reprex v2.0.2

for(z in c("c1","c2","c3")){
  assign(z, data.frame(matrix(NA, nrow = 3, ncol = 2)))
}
1 Like

When a more specific data frame is needed.

make_df <- function(x){
  layout = data.frame(id = as.integer(),
                      type = as.factor(as.character()),
                      reading = as.numeric())
  return(layout)
}
new_df <- c("df1","df2","df3")
for(i in seq_along(new_df)) assign(new_df[i],make_df(i))
ls()
#> [1] "df1"     "df2"     "df3"     "i"       "make_df" "new_df"
str(df1)
#> 'data.frame':    0 obs. of  3 variables:
#>  $ id     : int 
#>  $ type   : Factor w/ 0 levels: 
#>  $ reading: num

Created on 2023-06-01 with reprex v2.0.2

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.