输入表
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
Run Code Online (Sandbox Code Playgroud)
输出文件
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
Run Code Online (Sandbox Code Playgroud)
如何将一列中的3个文本替换为数字和空白?
"好在Reponse栏"到"1""在Reponse栏中不好"到"-1""在Reponse栏中未定义"到""
数据:
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))
Run Code Online (Sandbox Code Playgroud)
Rei*_*son 17
您可以通过更改因子的标签来执行此操作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
Run Code Online (Sandbox Code Playgroud)
凯瑟琳,您的问题仍然可以通过R中的一本非常基本的教科书来回答.请参阅Dirk在您之前的问题中的评论.
回答
如果d是您的数据框,那么:
d[d$Response == "Good",]$Response = 1
d[d$Response == "Bad",]$Response = -1
d[d$Response == "undefined",]$Response = ""
Run Code Online (Sandbox Code Playgroud)
我猜(我可能错了)"Undefined"缺少数据.在这种情况下,使用NA而不是空白.任何基础研发本书将介绍NA的
如果您的数据位于数据框中df
df$Response[df$Response == "Good"] <- 1
df$Response[df$Response == "Bad"] <- -1
df$Response[df$Response == "undefined"] <- ""
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30810 次 |
| 最近记录: |