Input table
Patients Hospital Drug Response
1 AAA a Good
1 AAA a Bad
2 BBB a Bad
3 CCC b Good
4 CCC c Bad
5 DDD e undefined
Output file
Patients Hospital Drug Response
1 AAA a 1
1 AAA a -1
2 BBB a -1
3 CCC b 1
4 CCC c -1
5 DDD e
How to replace 3 texts in one column to number and blank?
"good in Reponse column" to "1" "bad in Reponse column" to "-1" "undefined in Reponse column" to " "
Data:
structure(list(Patients = c(1L, 1L, 2L, 3L, 4L, 5L), Hospital = structure(c(1L,
1L, 2L, 3L, 3L, 4L), .Label = c("AAA", "BBB", "CCC", "DDD"), class = "factor"),
Drug = structure(c(1L, 1L, 1L, 2L, 3L, 4L), .Label = c("a",
"b", "c", "e"), class = "factor"), Response = structure(c(2L,
1L, 1L, 2L, 1L, 3L), .Label = c("Bad", "Good", "undefined"
), class = "factor")), .Names = c("Patients", "Hospital",
"Drug", "Response"), class = "data.frame", row.names = c(NA,
-6L))
You can do this with one line by changing 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
Catherine, your questions could still be answered by a very basic textbook in R. Please see Dirk's comment in your previous question.
Answer
If d
is your 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 wrong) that "Undefined" is missing data. In which case, use NA
rather than a blank. Any basic R book will describe NA
's
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"] <- ""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With