Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何对表或数据框中的计数运行中值函数?_R - Fatal编程技术网

R 如何对表或数据框中的计数运行中值函数?

R 如何对表或数据框中的计数运行中值函数?,r,R,我正在处理的数据样本如下所示:;请注意,原始数据帧要大得多: vehicle id trip code 1 abc 1 bcd 1 ghy 3 lhy 3 gfy 6 awl 6 rhr 我把它变成了一个表格,以便查看每辆独特车辆的出行频率 vehicle i

我正在处理的数据样本如下所示:;请注意,原始数据帧要大得多:

    vehicle id  trip code
       1          abc
       1          bcd
       1          ghy
       3          lhy
       3          gfy
       6          awl
       6          rhr
我把它变成了一个表格,以便查看每辆独特车辆的出行频率

  vehicle id    Trip (freq)
      1         3
      3         2
      6         2
我想计算每辆车的中位数,以便以后对其进行平均。我应该通过在数据帧上运行脚本来实现这一点,还是通过告诉R将3扩展为[1,2,3],将2扩展为[1,2],然后在这些扩展上运行中间值,在表中的频率上实现这一点

我考虑了以下代码,但我不确定这是否是最好的方法:

TRIPS_28$vehicle_id %>%
group_by(TRIPS_28$vehicle_id) %>%
median(count(TRIPS))

谢谢你的帮助

听起来你有这样的数据:

(dat <- data.frame(vehicleID = c(1, 3, 6), Trip = c(3, 2, 2)))
#   vehicleID Trip
# 1         1    3
# 2         3    2
# 3         6    2

我不明白你在计算什么?
dat$median <- (dat$Trip + 1) / 2
dat
#   vehicleID Trip median
# 1         1    3    2.0
# 2         3    2    1.5
# 3         6    2    1.5