简体   繁体   中英

error in recoding

Trying to recode my missing values to NA in R with the epicalc package, I get the following error:

 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:

use(.data)

before running recode . Now, if you read what is ?use , then you'll find this:

'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. The destination data frame is saved in memory, by default as '.data', and automatically attached to the search path. This setting is the basis for other commands of 'epicalc' including 'des', 'summ', 'recode', 'label.var' etc.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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