0

I am trying to create an empty data frame in R using the following code:

 data <- data.frame(ticks = numeric(0), identity = numeric(0), p-h = numeric(0), p-y = numeric(0), v-x = numeric(0), v-y = numeric(0), size = numeric(0), homo = numeric(0))

ANSWER TO ABOVE: change - to _ simply.

THOUGH I GET THE FOLLOWING ERROR WHEN ADDING DATA:

Error in value[[jvseq[[jjj]]]] : subscript out of bounds
In addition: Warning message:
In matrix(value, n, p) : data length exceeds size of matrix

Please Help. Thanks in advance.

Abhishek Bhatia
  • 9,404
  • 26
  • 87
  • 142
  • 1
    Are you creating this empty data.frame because you plan to fill it row by row? That's really not a good idea in R. There are almost always better alternatives. – MrFlick Jul 31 '14 at 04:05
  • @MrFlick. Yes I do. I corrected the code but I get the following error Error in value[[jvseq[[jjj]]]] : subscript out of bounds In addition: Warning message: In matrix(value, n, p) : data length exceeds size of matrix – Abhishek Bhatia Jul 31 '14 at 04:07
  • 1
    Well, now I have no idea what code you are running. But this general strategy sounds flawed. If you have a new question, start a new post. – MrFlick Jul 31 '14 at 04:09
  • @MrFlick OKay will do so but I can only after 90 minutes :(. It shows "You can only post once every 90 minutes." – Abhishek Bhatia Jul 31 '14 at 04:18
  • @MrFlick Please see the following link. http://stackoverflow.com/questions/25051528/error-in-adding-rows-to-an-empty-data-frame-in-r . Added it as a new question – Abhishek Bhatia Jul 31 '14 at 05:39

2 Answers2

3

p-h is "p minus h". Name your variable in a different way, i.e. p_h or ph.

sashkello
  • 17,306
  • 24
  • 81
  • 109
  • Please see the following link. http://stackoverflow.com/questions/25051528/error-in-adding-rows-to-an-empty-data-frame-in-r . Added it as a new question – Abhishek Bhatia Jul 31 '14 at 05:40
1

Not that you should, but you can use backticks to get your command to work:

 data <- data.frame(ticks = numeric(0), identity = numeric(0), 
   `p-h` = numeric(0), `p-y` = numeric(0), `v-x` = numeric(0), 
   `v-y` = numeric(0), size = numeric(0), homo = numeric(0))

But it changes the names:

#[1] ticks    identity p.h      p.y      v.x      v.y      size     homo    
#<0 rows> (or 0-length row.names)
Jota
  • 17,281
  • 7
  • 63
  • 93
  • Please see the following link. http://stackoverflow.com/questions/25051528/error-in-adding-rows-to-an-empty-data-frame-in-r . Added it as a new question – Abhishek Bhatia Jul 31 '14 at 05:41