1

Brand new to R; trying to get my data read in and reshaped properly. File format has seven columns of "id"-ish data, then about sixty columns of annual growth values, columns labelled by year. First pass was:

> firstData <- read.csv("~/theData.csv")  
> nuData <- melt(firstData, id=1:7)

That made the right arrangement but read.csv() had prepended an X to all the years ("X1983", e.g.), so now they don't work as values. I get that, so:

> firstData <- read.csv("~/theData.csv",check.names = FALSE)  
> nuData <- melt(firstData, id=1:7)  
Error in `[.data.frame`(data, , x) : undefined columns selected

The Xs were kept away (plain "1983", etc.), but now it won't melt(). Many retries; lots of reference-consulting; hard to figure out the right way to find the answer. It seems to think the structure is okay:

> is.data.frame(firstData)
[1] TRUE
> ncol(firstData)
[1] 71

I suspect that something about the bare-number column labels for 8-71 is throwing it. How do I reassure it that everything's fine?

EDIT
Didn't want to dump the data-mess if someone could answer offhand, but here's a sample. I thought I'd figured it out when I found spaces in column labels... but I fixed them and still get the same error. Is it a problem that the rows don't all have values in the 2016 column?

Tree,Gap,TransX,TransY,DBH,Nodes,Ht,1953,1954,1955,1956,1957,1958,1959,1960,1961,1962,1963,1964,1965,1966,1967,1968,1969,1970,1971,1972,1973,1974,1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,
1,1,3,0,4.4,23,366,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,7,3,3,7,3,4,4,13,7,23,17,34,25,30,23,19,25,22,29,28,20,14,6,
2,1,4,0,3.3,24,398,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,12,11,16,10,7,7,16,13,16,12,25,14,24,21,20,22,20,24,15,27,18,17,15,16,
3,1,5,2,2.8,24,325,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,5,7,16,8,6,16,18,10,17,7,21,10,14,12,16,14,23,15,21,20,14,14,12,9,
4,1,5,2.5,3.5,22,388,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,6,6,5,5,15,9,12,13,29,16,20,13,17,19,27,25,13,31,32,26,26,23,
5,1,10.2,0,9.5,43,739,,,,,,,,,,,,,,,,,,,,,16,18,9,14,18,13,14,10,6,8,8,10,12,11,13,11,6,6,7,8,8,9,11,13,20,27,17,23,11,38,21,29,27,31,29,19,23.1,22,33,40,24,22,24,
uhClem
  • 95
  • 10
  • this works `mt <- setNames(mtcars, 2001:2011); reshape2::melt(mt, id = 1:3)` so please provide a reproducible example – rawr Feb 07 '16 at 23:03
  • You could leave `check.names` as true and after the `melt` `gsub` the `"X"` and convert the column to numeric. – JeremyS Feb 08 '16 at 06:56
  • First chance I've had to get back to this. @rawr: my posted code actually reproduces relentlessly here while your counterexample works -- just as you say. It could be that the problem I'm having lies in the specifc example I gave, and not in an analogue. – uhClem Feb 09 '16 at 18:45
  • @JeremyS: thanks -- I was wondering how you do a string fix like that but got distracted by whatever is going on with the allegedly undefined columns. Probably I'll wind up resorting to this... but I WANT to understand the OTHER! Thanks to both of you for your inputs. – uhClem Feb 09 '16 at 18:46
  • @uhClem: It should be no problem that the rows don't all have values in the 2016 column: read.csv() fills those missings with NAs (you could check that in your firstData object) and they wont cause that error. From which package is your melt() function? – Arsak Feb 11 '16 at 23:01
  • btw: Did you see the similar questions and the comments there? [here](http://stackoverflow.com/questions/11670713/r-melt-error-undefined-columns?rq=1) and [here](http://stackoverflow.com/questions/14185928/r-reshape-melt-error?rq=1)? – Arsak Feb 11 '16 at 23:05
  • @Marzipanherz: Thanks! Still don't have an answer-answer but your here2 got me to understand I needed to step up to reshape2... for reasons that remain elusive. Also now there's a `Warning message: attributes are not identical across measure variables; they will be dropped` Can't tell whether that means something or nothing; data looks superficially fine. – uhClem Feb 11 '16 at 23:53
  • @uhClem: related to your new warning: [this](http://stackoverflow.com/questions/25688897/reshape2-melt-warning-message) questions has a nice long answer, which could be of help :) – Arsak Feb 12 '16 at 00:10
  • 1
    @Marzipanherz: Oho! Truly you have a heart of pure almond paste! Much of this remains mysterious but you've been a big help on my apparently unattractive question. Vielen Dank. – uhClem Feb 12 '16 at 01:06
  • Thanks, I'm glad I could help! – Arsak Feb 12 '16 at 01:59

0 Answers0