当前位置: 动力学知识库 > 问答 > 编程问答 >

r - xtabs with NA values in factors

问题描述:

Can someone explain why the first code works, but the second (my real data) don't?

dados2 <- data.frame(expand.grid(x = 1:5, y = c(1:3, NA)), z = rnorm(20, 10))

dados <- structure(list(ROP = structure(c(1L, 1L, 1L, 1L, 1L, 8L, 8L,

8L, 8L, 8L, 12L, 12L, 12L, 12L, 12L, 5L, 5L, 5L, 5L, 5L, 13L,

13L, 13L, 13L, 13L, 9L, 9L, 9L, 9L, 9L, 14L, 14L, 14L, 14L, 14L,

11L, 11L, 11L, 11L, 11L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L,

3L, 2L, 2L, 2L, 2L, 2L, 16L, 16L, 16L, 16L, 16L, 7L, 7L, 7L,

7L, 7L, 10L, 10L, 10L, 10L, 10L, 15L, 15L, 15L, 15L, 15L, 6L,

6L, 6L, 6L, 6L), .Label = c("Centro", "Centro-Sul", "Cristal",

"Cruzeiro", "Eixo-Baltazar", "Extremo-Sul", "Glória", "Humaitá / Navegantes e Ilhas",

"Leste", "Lomba do Pinheiro", "Nordeste", "Noroeste", "Norte",

"Partenon", "Restinga", "Sul"), class = "factor"), V0639 = structure(c(NA,

4L, 1L, 3L, 2L, NA, 4L, 3L, 1L, 2L, 1L, NA, 4L, 2L, 3L, NA, 1L,

4L, 2L, 3L, 4L, NA, 2L, 1L, 3L, NA, 4L, 1L, 2L, 3L, NA, 4L, 1L,

3L, 2L, 4L, NA, 2L, 1L, 3L, NA, 4L, 1L, 2L, 3L, NA, 4L, 1L, 2L,

3L, NA, 1L, 4L, 2L, 3L, NA, 4L, 1L, 2L, 3L, NA, 4L, 1L, 3L, 2L,

NA, 4L, 1L, 2L, 3L, NA, 4L, 1L, 2L, 3L, 4L, NA, 2L, 1L, 3L), .Label = c("Casamento civil e religioso",

"Só casamento civil", "Só casamento religioso", "União consensual"

), class = "factor"), N = c(53908.3751669145, 10087.3153340775,

2180.27746424467, 169.543509291214, 1179.48852547216, 7944.27668688213,

4198.0289075863, 85.8500512106339, 401.079414895287, 540.764939425649,

1252.27647475812, 22262.0079910393, 4502.77340653497, 1139.82982699682,

109.112300670887, 17011.6513260819, 1317.87354653619, 6588.12217044182,

412.937197974487, 147.415758965552, 6642.82854605454, 14987.6439959282,

774.969914075169, 958.022131346179, 127.535412595993, 19020.7752399327,

7406.00870501281, 1133.47419576721, 546.616474499807, 70.1253847878379,

22501.7403040535, 7254.62250909283, 763.76199990431, 87.0946658810724,

837.7805210683, 3567.76515943208, 5982.61620088674, 382.884248088654,

186.927288967051, 114.807102625521, 11129.5372351677, 4914.53362482472,

695.115244984714, 121.974869634082, 28.8390253888934, 4770.08301115204,

1588.090796661, 233.298370221274, 117.93782864403, 32.5899933216485,

19180.0565471618, 1212.24524028254, 6087.41337910638, 784.698098058887,

43.5867353905724, 13660.700403757, 4069.23853152611, 657.232605077846,

487.110414783771, 263.71804485504, 6949.90589785599, 3482.03751394504,

515.336097207438, 30.8637693511544, 135.856721640418, 10197.460343484,

5508.88020981299, 748.59957180971, 372.247806935029, 90.812067958561,

9839.12058314162, 5503.70032977132, 420.134219460268, 301.928439681743,

79.9783412709408, 2522.12437138921, 5272.21948508564, 474.062878298473,

267.845404209035, 44.7478610176534)), .Names = c("ROP", "V0639",

"N"), class = "data.frame", row.names = c(NA, -80L))

xtabs(z ~ ., dados2, exclude = NULL, na.action = na.pass)

xtabs(N ~ ., dados, exclude = NULL, na.action = na.pass)

网友答案:

You can apply the addNA function to your factor columns, or you can convert the columns to character before using xtabs.

Try:

dados[1:2] <- lapply(dados[1:2], addNA)
xtabs(N ~ ., dados, exclude = NULL, na.action = na.pass)

Or:

dados[1:2] <- lapply(dados[1:2], as.character)
xtabs(N ~ ., dados, exclude = NULL, na.action = na.pass)
分享给朋友:
您可能感兴趣的文章:
随机阅读: