用空列 returns 重塑为宽,只有一个观察

reshape to wide with empty column returns only one observation

我注意到一个空列最终只会导致观察结果。有人可以向我解释这种行为吗?我应该在继续之前查找并删除空列吗?

testdf <- CO2[,c("Plant","conc","Treatment","uptake")]

testdf$Plant <- gsub("c","n",testdf$Plant)

works <- reshape(
  testdf,
  idvar = c(
    "Plant",
    "conc"
  ),
  timevar = "Treatment",
  direction = "wide"
)

testdf$Problem <- as.numeric(NA)

notworking <- reshape(
  testdf,
  idvar = c(
    "Plant",
    "conc",
    "Problem"
  ),
  timevar = "Treatment",
  direction = "wide"
)

reshape 代码中第 94 行可能是问题所在

...
data[, tempidname] <- interaction(data[, idvar], 
            drop = TRUE)
...

使用 'testdf'

interaction(testdf[, c("Plant", "conc", "Problem")], drop = TRUE)
# [1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
#[23] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
#[45] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
#[67] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
#Levels: 

结果全部 NA

删除 'Problem' 列

interaction(testdf[, c("Plant", "conc")], drop = TRUE)
#[1] Qn1.95   Qn1.175  Qn1.250  Qn1.350  Qn1.500  Qn1.675  Qn1.1000 Qn2.95   Qn2.175  Qn2.250  Qn2.350  Qn2.500 
#[13] Qn2.675  Qn2.1000 Qn3.95   Qn3.175  Qn3.250  Qn3.350  Qn3.500  Qn3.675  Qn3.1000 Qn1.95   Qn1.175  Qn1.250 
#[25] Qn1.350  Qn1.500  Qn1.675  Qn1.1000 Qn2.95   Qn2.175  Qn2.250  Qn2.350  Qn2.500  Qn2.675  Qn2.1000 Qn3.95  
#[37] Qn3.175  Qn3.250  Qn3.350  Qn3.500  Qn3.675  Qn3.1000 Mn1.95   Mn1.175  Mn1.250  Mn1.350  Mn1.500  Mn1.675 
#[49] Mn1.1000 Mn2.95   Mn2.175  Mn2.250  Mn2.350  Mn2.500  Mn2.675  Mn2.1000 Mn3.95   Mn3.175  Mn3.250  Mn3.350 
#[61] Mn3.500  Mn3.675  Mn3.1000 Mn1.95   Mn1.175  Mn1.250  Mn1.350  Mn1.500  Mn1.675  Mn1.1000 Mn2.95   Mn2.175 
#[73] Mn2.250  Mn2.350  Mn2.500  Mn2.675  Mn2.1000 Mn3.95   Mn3.175  Mn3.250  Mn3.350  Mn3.500  Mn3.675  Mn3.1000
#42 Levels: Mn1.95 Mn2.95 Mn3.95 Qn1.95 Qn2.95 Qn3.95 Mn1.175 Mn2.175 Mn3.175 Qn1.175 Qn2.175 Qn3.175 ... Qn3.1000

替代方法包括 dcast 来自 reshape2data.table

data.table::dcast(testdf, Plant + conc + Problem ~ Treatment, value.var = 'uptake')

spread 来自 tidyr

library(tidyr)
spread(testdf, Treatment, uptake)