替换特定列“单词”进入数字或空白

发布于 2024-10-31 10:56:44 字数 1143 浏览 4 评论 0原文

输入表

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤独陪着我 2024-11-07 10:56:44

您可以通过更改因子 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         

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         
完美的未来在梦里 2024-11-07 10:56:44

凯瑟琳,你的问题仍然可以通过一本非常基本的 R 教科书来回答。请参阅德克在你的 上一个问题

回答

如果d 是您的数据框,那么:

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

我猜测(我可能是错的)“未定义”缺少数据。在这种情况下,请使用 NA 而不是空白。任何 R 基础书籍都会描述 NA

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

在巴黎塔顶看东京樱花 2024-11-07 10:56:44

如果您的数据位于数据框中df

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

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"] <- ""
迎风吟唱 2024-11-07 10:56:44

您可以使用简单的 ifelse() 语句。

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

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, ""))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文