Question

My data frame is as follows:

> t
     Day TestID VarID
1 2013-04-27  Total Total

> str(t)
'data.frame':   1 obs. of  3 variables:
 $ Day   : Date, format: "2013-04-27"
 $ TestID: factor [1, 1] Total
  ..- attr(*, "levels")= chr "Total"
 $ VarID : Factor w/ 3 levels "0|0","731|18503",..: 3

When I try doing a rbind I get the following error

> rbind(t,t)
Error in NextMethod() : invalid value

but when I try to recreate the data frame directly I don't get that error:

> t <- data.frame(Day = as.Date("2013-04-27"),TestID = "Total", VarID = "Total")
> t
     Day TestID VarID
1 2013-04-27  Total Total
> str(t)
'data.frame':   1 obs. of  3 variables:
 $ Day   : Date, format: "2013-04-27"
 $ TestID: Factor w/ 1 level "Total": 1
 $ VarID : Factor w/ 1 level "Total": 1
> rbind(t,t)
     Day TestID VarID
1 2013-04-27  Total Total
2 2013-04-27  Total Total

Can anyone help me figure out what is going on and how can I avoid this error. Thanks.

Was it helpful?

Solution

The major difference I see is that the TestID variable in the first version is factor [1, 1] (a matrix) rather than Factor (a vector)

First version:

t1 <- data.frame(Day = as.Date("2013-04-27"),
                 TestID = "Total", VarID = "Total")
rbind(t1,t1)

Convert to second version:

t2 <- t1
dim(t2$TestID) <- c(1,1)
str(t2$TestID)
##  factor [1, 1] Total
##  - attr(*, "levels")= chr "Total"
rbind(t2,t2)
## Error in NextMethod() : invalid value

Fix the mangled version:

t3 <- t2
t3$TestID <- drop(t3$TestID)
rbind(t3,t3)  ## works
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top