gpt4 book ai didi

将特定列 "words"替换为数字或空白

转载 作者:行者123 更新时间:2023-12-02 01:04:24 26 4
gpt4 key购买 nike

输入表

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

输出文件

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

如何将一列中的3个文本替换为数字和空白?

“响应列良好”改为“1”将“响应列中的错误”更改为“-1”“响应列中未定义”改为“”

数据:

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))

最佳答案

您可以通过更改因子响应的标签,用一行来完成此操作:

> 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

关于将特定列 "words"替换为数字或空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5612821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com