0

What I am trying to do is hopefully very simple. I have a vector of names that are to become the variable names in a data frame. The end result is a data frame with (initially) no data, but with 210 named variables as per the vector called "label". Any ideas as to how to do this?

vector0 <- c("a", "b", "c", "d", "e", "f", "g")
vector1 <- rep(1:3, times=1, each=70)
vector2 <- rep(1:5, times=1, each=14)
vector3 <- rep(1:2, times=1, each=7)
label <- paste(vector0, vector1, vector2, vector3, sep="")
aspark2020
  • 341
  • 2
  • 17
  • `df <- setNames(data.frame(matrix(NA, ncol = 210)), label)` would work ? Possible duplicate https://stackoverflow.com/questions/32712301/create-empty-data-frame-with-column-names-by-assigning-a-string-vector/ – Ronak Shah Aug 28 '19 at 05:14
  • It's not clear to me what the end result is. – Roman Luštrik Aug 28 '19 at 05:21

1 Answers1

1

You say "no data", so I suppose you want a data frame without any data, not even NA's:

df <- as.data.frame(replicate(210, numeric()))

Now you're ready to change the names:

names(df) <- label # `label` from OP

Or in one shot:

df <- setNames(as.data.frame(replicate(210, numeric())), label)
lebatsnok
  • 6,329
  • 2
  • 21
  • 22