1
votes

I want to create 100 empty data frames with names

  • df1, df2, ...,df100.

Each data frame will have 2 columns where

  • i'th data frame dfi have columns with colnames "yi" and "xi". For example, df5 's column names will be y5 and x5.
  • first column will be chracter and second one will be numeric.

How can I create such data frames using R. I will be very glad for any help. Many thanks.

1
Are these character or numeric columns?akrun
akrun, I edited the question.oercim

1 Answers

5
votes

We can create the empty 'data.frames' in a list using replicate and change the column names with Map

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]