尝试将列的值四舍五入到 R 中最接近的值时出错

Error when trying to round values of a column to nearest in R

这是我要分析的数据列:

dput(head(IdentifiedCars$ConnectionTimeHours, 10))
c(12.0102777777778, 0.00305555555555556, 6.29361111111111, 3.34416666666667, 
1.43361111111111, 2.54472222222222, 3.86694444444444, 14.3997222222222, 
1.3175, 1.75888888888889)

我正在尝试使用此脚本将数据框 (IdentifiedCars) 中列 (ConnectionTimeHours) 的值四舍五入为最接近的整数:

df <- IdentifiedCars %>%
  round(ConnectionTimeHours, 0) %>%
  group_by(ConnectionTimeHours) %>%
  summarise(counts = n())

不幸的是,我一直收到这个错误: function_list[i] 中的错误: 找不到对象 'ConnectionTimeHours'

有谁知道我该如何解决这个问题?

mutategroup_by 内舍入,参见示例:

library(dplyr)

mtcars %>% 
  mutate(grp_drat = round(drat, 0)) %>% 
  group_by(grp_drat) %>% 
  summarise(count = n())

mtcars %>% 
  group_by(grp_drat = round(drat, 0)) %>% 
  summarise(count = n())

# # A tibble: 3 x 2
#    grp_drat count
#      <dbl> <int>
# 1        3    13
# 2        4    18
# 3        5     1

或者使用count()代替n():

mtcars %>% 
  count(grp_drat = round(drat, 0))
#   grp_drat  n
# 1        3 13
# 2        4 18
# 3        5  1