ifelse() 从两个其他因子向量创建新的因子向量未返回预期值。

huangapple go评论63阅读模式
英文:

ifelse() to create new factor vector from two other factor vectors fails to return expected value

问题

这段代码中有一个小错误。在你的代码中,最后一个ifelse语句的测试条件是这样的:

ifelse(((Canal == "No Canal") & (QEW == "No QEW")), "perdition", "Neither")))

注意到你用了&代替了&,这可能是因为你在HTML或XML中使用&来表示&符号,但在R中直接使用&来表示逻辑与操作。所以,你应该将&改成&,就像这样:

ifelse(((Canal == "No Canal") & (QEW == "No QEW")), "perdition", "Neither"))

这样修改后,最后一个ifelse语句就会返回"Neither"而不是"perdition"了。

英文:

I am using conditional statements and ifelse() to create a new factor with four levels. However, only three of the four levels are correctly returned. What am I doing wrong?

Here are my data:

arf <- structure(list(Location = structure(c(1L, 2L, 3L, 5L, 4L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L), .Label = c("FORT ERIE", "GRIMSBY", 
"LINCOLN", "NIAGARA-ON-THE-LAKE", "NIAGARA FALLS", "PELHAM", 
"PORT COLBORNE", "ST. CATHARINES", "THOROLD", "WAINFLEET", "WELLAND", 
"WEST LINCOLN"), class = "factor"), Canal = structure(c(2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L), .Label = c("Canal", 
"No canal"), class = "factor"), QEW = structure(c(2L, 2L, 2L, 
2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c("No QEW", "QEW"
), class = "factor")), row.names = c(NA, -12L
), class = "data.frame")

Here is my code:

 View(arf)
arf$QEWcanal <- "test"
arf$QEWcanal <- with(arf, 
  ifelse(((Canal == "Canal") & (QEW == "QEW")), "Both",
  ifelse(((Canal == "No canal") & (QEW == "QEW")), "QEW only",
  ifelse(((Canal == "Canal") & (QEW == "No QEW")), "Canal only",
  ifelse(((Canal == "No Canal") & (QEW == "No QEW")), "Neither", "perdition")))))
View(arf)

It works for the first three cases, but the last case returns "perdition" instead of "Neither". However, when I replace the last line with ifelse(((Canal == "No Canal") & (QEW == "No QEW")), "perdition", "Neither"))))), it works.

This is confusing because ??ifelse says the useage is ifelse(test, yes, no). However, when I follow that useage (i.e., my first block of code above), my last ifelse() statement returns "perdition" and not "Neither." Why?

答案1

得分: 1

对于第四个条件,您的代码写成了 No Canal,但该列中的值是 "No canal"(小写 c)。如果调整一下就可以运行。

此外,我建议您在有多个条件时不要使用 ifelse()。对于这种情况,更适合使用 dplyr::case_when()。以下是您的代码可能会是这样的:

library(tidyverse)
  
arf |> 
  mutate(QEWcanal = case_when(
    Canal == "Canal" & QEW == "QEW" ~ "Both",
    Canal == "No canal" & QEW == "QEW" ~ "QEW only",
    Canal == "Canal" & QEW == "No QEW" ~ "Canal Only",
    Canal == "No canal" & QEW == "No QEW" ~ "Neither",
    TRUE ~ "peridition"
  ))
#>               Location    Canal    QEW   QEWcanal
#> 1            FORT ERIE No canal    QEW   QEW only
#> 2              GRIMSBY No canal    QEW   QEW only
#> 3              LINCOLN No canal    QEW   QEW only
#> 4        NIAGARA FALLS No canal    QEW   QEW only
#> 5  NIAGARA-ON-THE-LAKE No canal    QEW   QEW only
#> 6               PELHAM No canal No QEW    Neither
#> 7        PORT COLBORNE    Canal No QEW Canal Only
#> 8       ST. CATHARINES    Canal    QEW       Both
#> 9              THOROLD    Canal No QEW Canal Only
#> 10           WAINFLEET No canal No QEW    Neither
#> 11             WELLAND    Canal No QEW Canal Only
#> 12        WEST LINCOLN No canal No QEW    Neither
英文:

For the fourth condition you code says No Canal, but the values in that column say "No canal" (small caps c in canal). It works if you adjust that.

Furthermore, I'd recommend not to use ifelse() whenever you have more than one condition. dplyr::case_when() is much more suitable for these occasions. Here is what that could look like for your code:

library(tidyverse)
  
arf |> 
  mutate(QEWcanal = case_when(
    Canal == "Canal" & QEW == "QEW" ~ "Both",
    Canal == "No canal" & QEW == "QEW" ~ "QEW only",
    Canal == "Canal" & QEW == "No QEW" ~ "Canal Only",
    Canal == "No canal" & QEW == "No QEW" ~ "Neither",
    TRUE ~ "peridition"
  ))
#>               Location    Canal    QEW   QEWcanal
#> 1            FORT ERIE No canal    QEW   QEW only
#> 2              GRIMSBY No canal    QEW   QEW only
#> 3              LINCOLN No canal    QEW   QEW only
#> 4        NIAGARA FALLS No canal    QEW   QEW only
#> 5  NIAGARA-ON-THE-LAKE No canal    QEW   QEW only
#> 6               PELHAM No canal No QEW    Neither
#> 7        PORT COLBORNE    Canal No QEW Canal Only
#> 8       ST. CATHARINES    Canal    QEW       Both
#> 9              THOROLD    Canal No QEW Canal Only
#> 10           WAINFLEET No canal No QEW    Neither
#> 11             WELLAND    Canal No QEW Canal Only
#> 12        WEST LINCOLN No canal No QEW    Neither

huangapple
  • 本文由 发表于 2023年6月13日 01:53:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定