【发布时间】: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')