开发者

recoding character variables

I'm trying to recode a character variable to a numeric value.

The character variable looks like this:

b <- c("Your category choice is correct", 开发者_如何学运维"Your category choice is incorrect", ...

I tried the following:

b_recoded <- ifelse(b = "Your category choice is correct",  
c(1), c(0))

I get the following error:

unused argument(s) (b = "Your category choice is correct")

How can i get this to work? I'm trying to code "Your category choice is correct" as 1 and "Your category choice is incorrect" as 0.

Sorry for the basic question. I'm still learning.


If your variable is character, you can use regular expressions to match values:

p <- "Your category choice is"
s <- sample(c("correct", "incorrect"), 100, replace = TRUE)
b <- paste(p, s)
( foo <- ifelse(grepl(" correct$", b), 1, ifelse(grepl(" incorrect$", b), 0, NA)) )
  [1] 1 1 0 1 1 0 0 0 1 1 0 1 1 0 0 1 1 0 1 0 1 1 1 0 0 1 0 1 0 1 0 1 0 0 1 0 0
 [38] 1 1 1 1 0 0 1 0 0 0 0 1 1 0 1 0 0 1 1 1 1 0 0 1 1 0 0 1 1 1 0 0 0 0 0 1 1
 [75] 1 0 0 0 1 0 0 0 0 1 1 0 1 1 0 1 0 1 1 0 0 0 1 1 1 0


The problem in your ifelse statement is that you use a single equal sign for a logical expression. = is used for top level left assignment in R. In a function call this means that you assign the argument b to "Your category choice is correct".

To get a logical expression, you need to use two equal signs ==. The following code does work (using mropas data):

b <- c(rep("Your category choice is correct", 3),
        rep("Your category choice is incorrect", 5),
        rep("Your category choice is correct", 2))

b_recoded <- ifelse(b == "Your category choice is correct",  1, 0)

Also note that I omitted the c() functions as you don't need to combine single elements.

If you are starting with R, it might be useful to read through one of the introductory manuals or at least keep it as reference. Here is one that I liked when I learned R:

http://cran.r-project.org/doc/contrib/Paradis-rdebuts_en.pdf


data:

df <- c(rep("Your category choice is correct", 3),
        rep("Your category choice is incorrect", 5),
        rep("Your category choice is correct", 2))

This will change your df into a factor

df2 <- factor(df, labels = c(1,0))   

In the beginning the handling of factors may get a bit confusing. So if you rather prefer to keep it as class numeric or integer you can e.g. do

df3 <- df
df3[df3 == "Your category choice is correct"] <- 1
df3[df3 == "Your category choice is incorrect"] <- 0
df3 <- as.integer(df3)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜