R-creating multiple named data frames

I want to create 100 empty named data frames

  • df1, df2, ..., df100.

Each data frame will have 2 columns where

  • i'th data frame dfi have columns with code names "yi" and "xi". For example, column names df5 would be y5 and x5.
  • The first column will remove, and the second will be numeric.

How to create such data frames with R. I would be very happy for any help. Many thanks.

0


source to share


1 answer


We can create empty "data.frames" in the list with replicate

and change the column names toMap

n <- 100
lst <- replicate(n,data.frame(y=character(), x=numeric(),
                     stringsAsFactors=FALSE), simplify=FALSE)

names(lst) <- paste0('df', 1:n)
nmy <- paste0('y', 1:n)
nmx <- paste0('x', 1:n)
lst1 <- Map(function(x,y,z) {names(x) <- c(y,z); x}, lst, nmy, nmx)

      



or

lst1 <- Map(setNames, lst, as.data.frame(rbind(nmy,nmx)))


str(lst1, list.len=3)
#List of 100
# $ df1  :'data.frame': 0 obs. of  2 variables:
#  ..$ y1: chr(0) 
#  ..$ x1: num(0) 
# $ df2  :'data.frame': 0 obs. of  2 variables:
#  ..$ y2: chr(0) 
#  ..$ x2: num(0) 
# $ df3  :'data.frame': 0 obs. of  2 variables:
#  ..$ y3: chr(0) 
#  ..$ x3: num(0) 
# [list output truncated]

      

+4


source







All Articles