-1

I am trying to create a dataframe using code with the basic structure:

df <- data.frame(A = "a", B = "b", C = "c", D = "d", E = "e")

However once I go over 200 columns the code fails to finish reading, with a result something like this:

#complete code
#+
""
#Error: unexpected string constant in:
#(part of my code here)
#"""

I've rigorously checked my code for typos (which there are none) and no matter what section of the code I delete it starts to work once the number of columns goes under 200.

Any suggestions on how to make a dataframe with more than 200 columns? Thanks in advance!

emily
  • 59
  • 2
  • 8
  • 2
    Downvote for lack of any code that would demonstrate the concern. There might be a limit on the number of columns but it's far greater than 200. – IRTFM May 03 '16 at 01:15
  • Possible duplicate of [How to initialize empty data frame (lot of columns at the same time) in R](http://stackoverflow.com/questions/20501345/how-to-initialize-empty-data-frame-lot-of-columns-at-the-same-time-in-r) – MichaelChirico May 03 '16 at 01:26
  • I am working with data that is under ethical approval so cannot reproduce my code here and have tried to include a simplified example as the data is extremely complex, hence over 200 columns but I will in future try to include better working examples. – emily May 03 '16 at 01:46
  • 1
    Are you copying and pasting to the R command line? If so, it could just be that the text breaks after a certain number of characters. Try putting your text into a file, call it `import.R` or something and then do `source("import.R")`. This is a known issue - http://stackoverflow.com/questions/13216480/paste-character-limit – thelatemail May 03 '16 at 02:11
  • That fixes it, thanks! – emily May 03 '16 at 21:59

2 Answers2

1
DF <- data.frame(A = "a")

for (i in 2:300){
  DF[[paste0("V", i)]] <- runif(1)
}

ncol(DF)
# [1] 300

I would actually probably do this using setDT from data.table:

library(data.table)

DF <- setDF(lapply(integer(300), function(...) runif(10)))
ncol(DF)
# [1] 300
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
1

To create a data.frame with 200 columns :

df <- as.data.frame(do.call(cbind, as.list(1:200)))
HubertL
  • 19,246
  • 3
  • 32
  • 51