我注意到一个空列最终会导致仅观察。有人可以向我解释这种行为吗?在继续之前,我应该只查找并删除空列吗?
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"
)
答案 0 :(得分:1)
第94行&#39;&#39;在reshape
代码中可能会导致问题
...
data[, tempidname] <- interaction(data[, idvar],
drop = TRUE)
...
使用&#39; testdf&#39;
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
。
删除&#39;问题&#39;柱
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
或reshape2
的{{1}}
data.table
data.table::dcast(testdf, Plant + conc + Problem ~ Treatment, value.var = 'uptake')
来自spread
tidyr