录音错误

时间:2013-02-25 17:35:52

标签: r

尝试使用NA包将我的缺失值重新编码为R中的epicalc,我收到以下错误:

 recode(trstlglR, 99 , NA, dataFrame=ESSround5)
 Error in search()[[pos]] : attempt to select more than one element

虽然命令似乎做了我想要的,但我担心我会遗漏一些东西。数据框太大,无法检查每个值。有人有这方面的经验吗?

可复制的例子:

structure(list(trstlglR = c(0L, 0L, 0L, 1L, NA, NA, NA, NA, 0L, 
0L), trstplcR = c(0L, 0L, 0L, 0L, 0L, NA, NA, 0L, 0L, 0L), plcarcrR = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, NA, NA)), .Names = c("trstlglR", 
"trstplcR", "plcarcrR"), row.names = c(1L, 2L, 3L, 5714L, 2450L, 
2980L, 3837L, 6136L, 2197L, 2198L), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

如果您查看?recode,示例首先会执行:

use(.data)

在运行recode之前。现在,如果您阅读的是?use,那么您会发现:

  

'use'从Dbase(.dbf),Stata(.dta),SPSS(.sav),EpiInfo(.rec)和逗号分隔值(.csv)格式以及R数据帧中读取数据集。目标数据框保存在内存中,默认为“.data”,并自动附加到搜索路径。 此设置是'epicalc'其他命令的基础,包括'des','summ','recode','label.var'等。

所以你需要做的是:

set.seed(45)
df <- data.frame(x=sample(1:3, 20, replace=T), y=sample(20))

use(df) # first to copy this to .data and attach.
recode(x, 2, NA, df) # not it should work without errors

#     x  y
# 1  NA 15
# 2  NA  6
# 3  NA  3
# 4   3  8
# 5  NA  1
# 6  NA 16
# 7   3  5
# 8   3  9
# 9   1 10
# 10  3 20
# 11 NA 11
# 12  1  4
# 13 NA  2
# 14 NA 12
# 15  1 13
# 16  3 17
# 17 NA 18
# 18  3 19
# 19  1  7
# 20  1 14
相关问题