[ACCEPTED]-Replace specific column "words" into number or blank-numbers

Accepted answer
Score: 17

You can do this with one line by changing 1 the labels of the factor Response:

> within(df, Response <- factor(Response, labels = c(-1, 1, "")))
  Patients Hospital Drug Response
1        1      AAA    a        1
2        1      AAA    a       -1
3        2      BBB    a       -1
4        3      CCC    b        1
5        4      CCC    c       -1
6        5      DDD    e         
Score: 5

Catherine, your questions could still be 6 answered by a very basic textbook in R. Please 5 see Dirk's comment in your previous question.

Answer

If d is your 4 data frame, then:

d[d$Response == "Good",]$Response = 1
d[d$Response == "Bad",]$Response = -1
d[d$Response == "undefined",]$Response = ""

I'm guessing (I may be 3 wrong) that "Undefined" is missing 2 data. In which case, use NA rather than a 1 blank. Any basic R book will describe NA's

Score: 3

If your data is in a data frame df

df$Response[df$Response == "Good"] <- 1
df$Response[df$Response == "Bad"] <- -1
df$Response[df$Response == "undefined"] <- ""

0

Score: 2

You can use a simple ifelse() statement.

cath <- data.frame(nmbrs = runif(10), words = sample(c("good", "bad"), 10, replace = TRUE))
cath$words <- ifelse(cath$words == "good", 1, ifelse(cath$words == "bad", -1, ""))

0

More Related questions