【问题标题】:Replace specific column "words" into number or blank将特定列“单词”替换为数字或空白
【发布时间】:2011-08-02 12:56:35
【问题描述】:

输入表

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

【问题讨论】:

  • 我认为 Gavin 输入了样本数据。

标签: r replace numbers


【解决方案1】:

如果您的数据在数据框中df

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

【讨论】:

    【解决方案2】:

    Catherine,您的问题仍然可以通过 R 中非常基本的教科书来回答。请参阅 Dirk 在您的previous question 中的评论。

    回答

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

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

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

    【讨论】:

      【解决方案3】:

      您可以使用简单的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, ""))
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • +1 不错!当然,Response 必须是一个因素(可能是)。
        • 或者,sapply(Data$Response,switch,'Good'=1,'Bad'=-1,'undefined'="") 是一种更通用的方法,但它肯定更慢。另外,使用 switch 时,Data$Response 必须是字符向量,否则会得到错误的结果。
        • 如何在不覆盖原始数据框的情况下做到这一点?
        • @stackoverflowuser2010(所以现在你想要我的 R 建议吗?:-) 如果你想在 within() 的输出中创建名为 Response 的向量,只需 @ 987654327@ 会做到的。如果你想要一个新的数据框,那么只需将within() 的结果分配给一个新对象df2 &lt;- within(df, Response &lt;- factor(Response, labels = c(-1, 1, "")))within()(和类似的transform())是交互式使用的便利函数,它们只返回修改后的数据框,因此将结果分配给使用的数据框以外的其他内容将产生一个新的数据框。
        • @GavinSimpson 我对您在输出中创建一个名为 Response 的向量的回答有点困惑。我想在我的数据框中有一个新的列(向量),现在已编码。within(df, obs &lt;- factor(df$Response, labels = c(1,0,-1))) 它不会添加新的烫发。柱子。它会生成一个临时列表。所以你仍然需要写一个新对象或覆盖旧对象。还是我错过了什么?
        猜你喜欢
        • 1970-01-01
        • 2014-05-20
        • 2022-11-13
        • 1970-01-01
        • 1970-01-01
        • 2022-12-06
        • 1970-01-01
        • 2020-09-14
        • 2012-05-06
        相关资源
        最近更新 更多