【问题标题】:Conditionally select column values in dplyr and then changing the datatype有条件地选择 dplyr 中的列值,然后更改数据类型
【发布时间】:2016-08-31 06:10:51
【问题描述】:

我在 R 中有以下数据框

   Lead.Stage       Number.of.Followup.Calls
1 Not Interested             Select
2  Unreachable                  ""
3   Qualified                   1
4  Unreachable                  2
5   Qualified                   2
6   Junk Lead                Select       

Number.of.Followup.Calls 是字符类型。我想在 Lead.Stage 上执行 groupby 来计算该 Lead.Stage 的平均跟进电话次数

在 dplyr 中,我过滤掉了Select and empty String,然后将数字转换为数字一。我在 r 中使用以下代码,但它似乎不起作用。

train %>% 
  group_by(Lead.Stage)  %>%
  filter((Number.of.Followup.Calls == "" | Number.of.Followup.Calls ==  
  "Select")) %>% 
  mutate_each_(funs(as.numeric), Number.of.Followup.Calls)  %>% 
  summarise(Total = mean(Number.of.Followup.Calls)) 

提前致谢:)

【问题讨论】:

  • 您有第二个factor 列吗?如果是这样,请train %>% group_by(Lead.Stage) %>% filter(as.character(Number.of.Followup.Calls)==
  • 此过滤器保留那些非数字的后续跟进数量。您可能希望过滤器反过来,以便只有有效数字通过。在过滤条件中插入否定 !。如果这不起作用,请提供示例数据以使您的代码可重现。
  • @akrun 两者都不是字符类型。
  • @Bernhard 当我在过滤器中使用否定时,它给了我以下错误`没有适用于“过滤器_”的方法应用于“逻辑”类的对象`这就是我使用它的方式filter(!(Number.of.Followup.Calls == "") | (Number.of.Followup.Calls == "Select"))
  • 试试filter(Number.of.Followup.Calls != '' & Number.of.Followup.Calls != 'Select')

标签: r dplyr


【解决方案1】:

使用%in% 更容易做到这一点

train %>% 
    group_by(Lead.Stage)  %>%
    filter(!Number.of.Followup.Calls %in% c("", "Select")) %>%
    summarise(Total = mean(as.numeric(Number.of.Followup.Calls)))
#   Lead.Stage Total
#       <chr> <dbl>
#1   Qualified   1.5
#2 Unreachable   2.0

否则,我们不需要执行所有 filter 和其他操作,因为转换为 as.numeric 会自动将所有非数字元素更改为 NA,然后只需执行 mean(., na.rm = TRUE)

train %>% 
    group_by(Lead.Stage)  %>%
    summarise(Total = mean(as.numeric(Number.of.Followup.Calls), na.rm = TRUE)) %>%
    na.omit()
#    Lead.Stage Total
#        <chr> <dbl>
# 1   Qualified   1.5
#2 Unreachable   2.0
#Warning messages:
#1: In mean(as.numeric(c("", "2")), na.rm = TRUE) :
# NAs introduced by coercion

warning message 只是一个关于将non-numeric 元素转换为NA 的友好提醒。

【讨论】:

    猜你喜欢
    • 2019-03-29
    • 2021-04-03
    • 2013-12-27
    • 2013-02-16
    • 2016-04-23
    • 2019-09-11
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    相关资源
    最近更新 更多