[R] Errors melt()ing data...

hadley wickham h.wickham at gmail.com
Thu Feb 28 14:15:49 CET 2008


Hi Neil,

>  ########Start Session 2##########
>  ## Now generate polar co-ordinates
>  t.norm1$polar.1 <- log10(sqrt(t.norm1$Height.1^2 + t.norm1$Height.2^2))
>  t.norm1$polar.2 <- atan((t.norm1$Height.2 / t.norm1$Height.1))
>  ## And cast the polar data
>  > t <- melt(subset(t.norm1, select= c("Sample.Name", "SNP", "Pool", "polar.1", "polar.2")), id=c("Sample.Name", "SNP"))
>  Error in if (!missing(id.var) && !(id.var %in% varnames)) { :
>   missing value where TRUE/FALSE needed
>  > traceback()
>  4: melt_check(data, id.var, measure.var)
>  3: melt.data.frame(as.data.frame(data), id = attr(data, "idvars"))
>  2: melt.cast_df(subset(t.norm1, select = c("Sample.Name", "SNP",
>        "Pool", "polar.1", "polar.2")), id = c("Sample.Name", "SNP"),
>        measure = c("polar.1", "polar.2"))
>  1: melt(subset(t.norm1, select = c("Sample.Name", "SNP", "Pool",
>        "polar.1", "polar.2")), id = c("Sample.Name", "SNP"), measure =
>  c("polar.1",
>        "polar.2"))
>  ########Finish Session 2##########
>
>  As far as I can tell the error is occurring within melt_check() where
>  there is a check to see if the id.var is missing and whether the
>  id.var exists within the data frames names, both of which are true
>  since the subset() call works fine on its own...
>
>  ########Start Session 3##########
>  > test <- subset(t.norm1, select= c("Sample.Name", "SNP", "Pool", "polar.1", "polar.2"))
>  > names(test)
>  [1] "Sample.Name" "SNP"         "Pool"        "polar.1"     "polar.2"
>  ########Start Session 3##########
>
>  What I find particularly strange is that there isn't really any
>  difference between
>  ########Session 1
>  > t          <- melt(t.norm1, id = c("Sample.Name", "SNP"))
>
>  ....and
>  ########Session 2
>  t <- melt(subset(t.norm1, select= c("Sample.Name", "SNP", "Pool",
>  "polar.1", "polar.2")), id=c("Sample.Name", "SNP"))
>
>  ..since I've done nothing to alter the "Sample.Name" and "SNP"
>  columns, all thats changing is the names of the two columns that are
>  the measure.var which in this instance is everything thats not defined
>  as being and id.var in the call to melt().
>
>  If anyone can provide any insight to what I'm doing wrong I'd be very grateful.

I think the problem is that reshape adds some extra information to the
cast data.frame, but this info is no longer relevant when you've
removed some of the columns.  Try as.data.frame to strip off this
extra info.

 t <- melt(subset(as.data.frame(t.norm1), select= c("Sample.Name",
"SNP", "Pool", "polar.1", "polar.2")), id=c("Sample.Name", "SNP"))

(Also, can't you get to cast.height.norm1 directly from norm1 ?
cast.height.norm1 <- cast(norm1, SNP ~ Sample.Name + variable, sum)

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list