Whis this data frame,
df <- expand.grid(id=\"01\", parameter=c(\"blood\", \"saliva\"), visit=c(\"V1\", \"V2\", \"V3\"))
df$value <- c(1:6)
df$sex <- rep
The relevant part of the reshape
code would be the line:
data[, tempidname] <- interaction(data[, idvar], drop = TRUE)
Look at how interaction
works:
> interaction("A", "B")
[1] A.B
Levels: A.B
> interaction("A", "B", NA)
[1]
Levels:
But, compare what would happen if NA
were retained as a level
:
> interaction("A", "B", addNA(NA))
[1] A.B.NA
Levels: A.B.NA
Thus, if you want to have the same result with base R's reshape
, you need to make sure that any "idvar" columns have NA
retained as a level.
Example:
df$sex <- addNA(df$sex)
reshape(df,
timevar="visit",
idvar=c("id", "parameter", "sex"),
direction="wide")
# id parameter sex value.V1 value.V2 value.V3
# 1 01 blood 1 NA NA
# 2 01 saliva 2 NA NA
Of course, the other question is how NA
can be treated as an identifying variable :-)