简体   繁体   English

重新编码错误

[英]error in recoding

Trying to recode my missing values to NA in R with the epicalc package, I get the following error: 尝试使用epicalc包将我的缺失值重新编码为R中的NA ,我收到以下错误:

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

Although the command seems to do what I want, I am afraid I am missing something. 虽然命令似乎做了我想要的,但我担心我会遗漏一些东西。 The dataframe is too large to check every value. 数据框太大,无法检查每个值。 Anyone have any experience with this? 有人对此有经验吗?

Replicable example: 可复制的例子:

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")

If you look at ?recode , the examples first do: 如果你看一下?recode ,这些例子首先做:

use(.data)

before running recode . 在运行recode之前。 Now, if you read what is ?use , then you'll find this: 现在,如果您阅读了什么是?use ,那么您会发现:

'use' reads in datasets from Dbase (.dbf), Stata (.dta), SPSS(.sav), EpiInfo(.rec) and Comma separated value (.csv) formats as well as R data frames. 'use'从Dbase(.dbf),Stata(.dta),SPSS(.sav),EpiInfo(.rec)和逗号分隔值(.csv)格式以及R数据帧中读取数据集。 The destination data frame is saved in memory, by default as '.data', and automatically attached to the search path. 目标数据框保存在内存中,默认为“.data”,并自动附加到搜索路径。 This setting is the basis for other commands of 'epicalc' including 'des', 'summ', 'recode', 'label.var' etc. 此设置是'epicalc'的其他命令的基础,包括'des','summ','recode','label.var'等。

So what you'll have to do is: 所以你要做的是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM