【问题标题】:Apply if else condition to make new column in r应用 if else 条件以在 r 中创建新列
【发布时间】:2021-08-28 18:05:07
【问题描述】:

我想使用 if else 语句根据另一列中的数据在我的数据框中创建一个新列。我查看了一些先前的(例如this onethis one),但似乎做错了什么,因为我收到错误或没有新列。

我尝试过创建一个 ifelse 函数:

  if(x >= 4000)
{print (">4000")
  } else if (x >=3000 & x <= 4000) 
    {print ("3000-4000")
    } else if  (x >=2000 & x <= 3000) 
    {print("2000-3000")
      } else if (x >=1000 & x <= 2000)
      {print("1000-2000")
      } else print ("<1000")}

这个函数工作/运行,但我不知道如何将它应用到我的数据框中的一列(我试过这个dat$P.bins &lt;- Bins(dat$Pcol),但得到以下错误:条件长度> 1,只有第一个将使用元素1">4000"

我也尝试过运行 ifelse 语句:

dat$P.bin<- ifelse(P.col>=4000, ">4000",
                                ifelse(P.col <=4000 & >= 3000, "3000-4000"),
                                ifelse(P.col<=3000 & >= 2000, "2000-3000"), 
                                ifelse(P.col <=2000 & >=1000, "1000-2000"), 
                                ifelse(P.col <1000, "1000"))

但出现此错误:错误:意外'>=' in:"dat$P.bins =4000, ">4000",felse(Pcol =". With this语句我不确定如何在 ifelse 语句中做一个范围。

任何帮助或指导将不胜感激!

【问题讨论】:

  • 只需将您的代码包装在函数定义中。如my_function&lt;-function(x) { if (x&gt;=4000) {"&gt;4000} else if .......。然后拨打my_function(yourdataframe$yourcolumn)
  • 当我尝试这样做并运行上面的代码时,我得到了这些错误:if (x == "&gt;4000") { : argument is of length zero 中的错误和`条件长度> 1,并且只使用第一个元素[1] ">4000 ” `我该如何解决这个问题?我不确定参数的长度为零,也不确定为什么 print 参数不能正常工作。
  • 当我在运行代码并收到错误后查看我的数据框时,它添加了一个新列,但所有值都 >4000(没有将 P.col 中的值放入新列)。当我尝试从我的函数中删除该行时,我仍然得到相同的错误,但下一个值下降。只能在 if else 语句的 print 函数中输入数字吗?

标签: r if-statement nested dplyr


【解决方案1】:

我们可以像这样使用case_when:

library(tidyverse)

dat <- tibble(P.col = seq(0, 20000, 1000))

mutate(dat, P.bin = case_when(P.col >= 4000 ~ ">4000",
                              P.col <= 3000 & P.col >= 2000 ~ "2000-3000",
                              P.col <= 3000 & P.col >= 2000 ~ "2000-3000",
                              P.col <= 2000 & P.col >=1000 ~ "1000-2000",
                              P.col < 1000 ~ "1000"))
#> # A tibble: 21 x 2
#>    P.col P.bin    
#>    <dbl> <chr>    
#>  1     0 1000     
#>  2  1000 1000-2000
#>  3  2000 2000-3000
#>  4  3000 2000-3000
#>  5  4000 >4000    
#>  6  5000 >4000    
#>  7  6000 >4000    
#>  8  7000 >4000    
#>  9  8000 >4000    
#> 10  9000 >4000    
#> # … with 11 more rows

reprex package (v2.0.0) 于 2021-06-11 创建

【讨论】:

  • 我正在尝试将相同的代码格式应用于我的数据框中的另一列,代码运行并说它添加了一个新列,但是当我查看数据框时,新列不存在,相反,它在数据框中重复了每一行,并在名称旁边添加了新的列标题(例如 P.bin.Pcol)。你对我如何解决这个问题有什么建议吗?找不到答案...我尝试重新启动 R 并重新运行代码,但它不起作用。当我运行你提供的代码时,它现在也在这样做。
【解决方案2】:

您使用的ifelse 方法是正确的,但存在一些语法问题。

  • 您没有在正确的位置关闭括号。
  • ifelse 中未提及数据框名称。 P.col 本身是不够的。
  • P.col &lt;=4000 &amp; &gt;= 3000 无效。你需要P.col &lt;=4000 &amp; P.col &gt;= 3000

试试下面的代码 -

dat$P.bin<- with(dat, ifelse(P.col>=4000, ">4000",
                   ifelse(P.col <=4000 & P.col >= 3000, "3000-4000",
                   ifelse(P.col<=3000 & P.col >= 2000, "2000-3000", 
                   ifelse(P.col <=2000 & P.col >=1000, "1000-2000", 
                   ifelse(P.col <1000, "1000", NA_character_))))))

话虽如此,按照@jpdugo17 的建议使用case_when 可能是更清洁的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 2020-12-21
    • 2016-10-13
    相关资源
    最近更新 更多