I'm creating a new data.frame by doing the opposite of "flattening" an input data.frame (in other words going from "wide" to "narrow", creating more rows).
I'll be looping over columns of the input data.frame, and appending to the output data.frame. I know it's more efficient to create the full output data.frame outright and fill it within the loop, but my question is why is it possible to create a 0 x 4 data.frame, but apparently not possible to name those 4 columns... Thanks.
dff <- data.frame()
dim( dff ) <- c(0,4)
colnames(dff) <- c("first","second","third","fourth")
Error in `colnames<-`(`*tmp*`, value = c("first", "second", "third", "fourth" :
'names' attribute [4] must be the same length as the vector [0]