reshape from base vs dcast from reshape2 with missing values

后端 未结 1 1594
故里飘歌
故里飘歌 2021-01-21 02:57

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         


        
相关标签:
1条回答
  • 2021-01-21 03:41

    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] <NA>
    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 <NA>        1       NA       NA
    # 2 01    saliva <NA>        2       NA       NA
    

    Of course, the other question is how NA can be treated as an identifying variable :-)

    0 讨论(0)
提交回复
热议问题