2014-03-25 59 views
4

我收到以下錯誤:不能使用`填= NA`在鑄造

dcast(rep.data, country ~ variable, mean, fill=NA) 

Error in vapply(indices, fun, .default) : values must be type 'logical', 
but FUN(X[[4]]) result is type 'double' 

這裏是rep.data數據幀以上:

structure(list(id = c("MRT1996", "BFA1982", "LBR1990", "UKR2000", 
"MNG1993", "VEN1992", "OMN1987", "LAO1996", "OMN1982", "SYR1993" 
), wbcode = c("MRT", "BFA", "LBR", "UKR", "MNG", "VEN", "OMN", 
"LAO", "OMN", "SYR"), polcode = c("MAA", "BFO", "LBR", "UKR", 
"MON", "VEN", "OMA", "LAO", "OMA", "SYR"), country = c("Mauritania", 
"Burkina Faso", "Liberia", "Ukraine", "Mongolia", "Venezuela, RB", 
"Oman", "Lao PDR", "Oman", "Syrian Arab Republic"), cid = c(117L, 
14L, 96L, 174L, 115L, 179L, 130L, 94L, 130L, 161L), year = c(1996L, 
1982L, 1990L, 2000L, 1993L, 1992L, 1987L, 1996L, 1982L, 1993L 
), variable = structure(c(9L, 7L, 22L, 26L, 18L, 24L, 10L, 7L, 
16L, 25L), .Label = c("fdi", "gdp", "l.gdp", "gdp.pc", "l.gdp.pc", 
"growth", "govex", "deficit", "r.usa", "polity", "polpersist", 
"varpol.5", "bonds", "eqty", "net.port.in", "portin.gdp", "external.debt", 
"external.debt.gdp", "official.xr.pct", "inflation.deflt", "IIR.annual", 
"default", "cum.default.60", "pct.default.60", "cum.default.80", 
"pct.default.80", "TOR80s", "TOR90s", "events", "TOR5", "turn" 
), class = "factor"), value = c(6.2, 15.56, 1, 14.29, 74.04, 
27.27, 0, 7.92, 0, 0)), .Names = c("id", "wbcode", "polcode", 
"country", "cid", "year", "variable", "value"), row.names = c(26927L, 
18978L, 67240L, 81096L, 55030L, 74901L, 30322L, 20280L, 49085L, 
77731L), class = "data.frame") 
+0

那麼問題是什麼? –

回答

12

使用NA_real_代替NA,由於dcast比賽類型以及。

+1

工作!哇 - 但你能詳細說明一下嗎?我從未聽說過'NA_real_' – Heisenberg

+1

'NA'是一個奇怪的野獸,因爲它必須能夠同時引用和使用多種類型('字符','數字'等)。 'NA_real_'等同於'as.numeric(NA)'並強制'class(NA_real_)=「numeric」'。另一方面,'class(NA_character_)=「character」'。其他人是'NA_integer_'和'NA_complex_'。當使用例如'RCpp'連接'C++'時,還有'NA_STRING'(如[此代碼](https://github.com/robertzk/mungebitsTransformations/blob/master/src/numeric_to_factor.cpp# L104)) –