dplyr group_by就像安排一样,为什么?

时间:2015-12-23 02:01:11

标签: r dplyr

很抱歉,但我试图搜索我的主题问题,但我找不到答案,也许它太基本了。如果这太新手了,请道歉。

我正在尝试按DFVisitNumber列进行分组。我只能这样安排:

   TripType VisitNumber Weekday
      (int)       (int)  (fctr)
1       999           5  Friday
2        30           7  Friday
3        30           7  Friday
4        26           8  Friday
5        26           8  Friday
6        26           8  Friday
7        26           8  Friday

我想得到:

Count   TripType VisitNumber Weekday
       (int)       (int)  (fctr)
1       999           5  Friday
2        30           7  Friday
4        26           8  Friday

我写了这段代码:

train%>%
  select(TripType,VisitNumber,Weekday)%>%
  group_by(VisitNumber)

2 个答案:

答案 0 :(得分:0)

谢谢大家,我找到了解决方案。

我改为使用此代码:

train%>%
  select(TripType,VisitNumber,Weekday)%>%
  group_by(TripType,VisitNumber,Weekday)%>%
  summarise(count=n())

此致

答案 1 :(得分:0)

好的,这是一种相当花哨的方式,但是你走了:

TripType <- c(999, 30, 30, 26, 26, 26, 26)
VisitNumber <- c(5, 7, 7, 8, 8, 8, 8)
Weekday <- rep('Friday', 7)

data <- data.frame(TripType, VisitNumber, Weekday)

count <- table(VisitNumber)
data <- data.frame(unique(data), count)
data$VisitNumber.1 <- NULL

data
##   TripType VisitNumber Weekday Freq
##1      999           5  Friday    1
##2       30           7  Friday    2
##4       26           8  Friday    4