如何按行中的行和列汇总数据

时间:2017-02-13 20:52:47

标签: r

我有以下问题。我有一张数据表如下:

enter image description here

并希望创建一个如下所示的摘要: enter image description here

在excel中,它是简单的数据透视表,但如何在R?

中进行

1 个答案:

答案 0 :(得分:1)

library(dplyr)
cardata <- data.frame(country = c("poland", "germany", "austria",
                              "poland","poland", "austria",
                              "germany","germany"),
                  car = c("Fiat", "Fiat", "BMW", "Fiat", "BMW",
                          "Toyota", "Toyota", "Toyota"),
                  Age = c(5, 4, 2, 6, 6, 2, 1, 8),
                  Km = c(12e4, 10e4, 22e3, 14e4, 15e4, 52e3, 21e3, 22e4))

cardata %>%
group_by(country, car) %>%
summarise(AvgAge = mean(Age),
          AvgKm = mean(Km))
相关问题