replace loops with apply family functions (or dplyr), using logical functions in R

后端 未结 1 1500
旧时难觅i
旧时难觅i 2021-01-24 13:13

I have created this representative data frame that assigns condition categories using a for loop.

df <- data.frame(Date=c(\"08/29/2011\", \"08/29/2011\", \"         


        
1条回答
  •  生来不讨喜
    2021-01-24 13:40

    You could use case_when from dplyr:

    df1<- df %>%
    mutate(Accuracy= case_when(
      .$Diff <=  3 ~ "Excellent",
      .$Diff <=  10  ~ "Good",
      .$Diff <=  15  ~ "Fair",
      .$Diff <=  30  ~ "Poor",
      .$Diff >   30  ~ "Unpublishable",
      TRUE  ~"TBD")
    )
    
     df1
            Date  Time Diff      Accuracy
    1 08/29/2011 09:45  0.2     Excellent
    2 08/29/2011 10:00  4.3          Good
    3 08/30/2011 13:00  6.5          Good
    4 08/30/2011 13:30 15.0          Fair
    5 08/30/2011 10:14 16.5          Poor
    6 08/29/2012  9:09 31.0 Unpublishable
    7 08/29/2012 11:23 30.2 Unpublishable
    8 01/15/2012 17:06 21.9          Poor
    9 08/29/2012 12:20  1.9     Excellent
    

    0 讨论(0)
提交回复
热议问题