首页 > 解决方案 > 总结R中的某一列

问题描述

       ID     Company Price     Country      City
1  138761        GHI  1320 Netherlands Amsterdam
2  571119        GHI  2060 Netherlands Amsterdam
3  112503        DEF  2310 Germany     Berlin
4  885592        DEF  2060 France      Paris
5  825832        ABC  1800 Netherlands Amsterdam
    ...................

对不起,伙计们,我是 R 的新手。如果你能告诉我如何计算?我想计算每个城市每家公司的总收入。例如,在柏林,GHI 公司每月赚 13000 欧元。DEF 公司每月赚 22000 欧元,ABC 公司每月赚 8000 欧元,在阿姆斯特丹,......

对不起我之前的错误!

这是数据的一瞥。

glimpse(data)
Observations: 37,245
Variables: 28
$ ID                   <int> 420834, 138761, 571119, ...
$ Company              <fct> ABC, DEF, GHI
$ Price                <int> 970, 1320, 2060, 2480, 1...
$ Country              <fct> Netherlands, Netherlands...
$ City                 <fct> Amsterdam, Amsterdam, Am...
$ Neighbourhood        <fct> Oostelijk Havengebied - ...
$ Property_type        <fct> Apartment, Townhouse, Ap...
$ Room_type            <fct> Private room, Entire hom...
$ Bathrooms            <dbl> 1.5, 1.0, 1.0, 1.0, 1.0,...
$ Bedrooms             <int> 1, 1, 1, 1, 1, 1, 1, 1, ...
$ Beds                 <int> 2, 1, 1, 1, 2, 1, 1, 1, ...
$ Bed_type             <fct> Real Bed, Real Bed, Real...
$ Review_scores_rating <int> 97, 87, 100, 99, 93, 97,...
$ Aircon               <fct> 0, 0, 0, 0, 0, 0, 0, 0, ...
$ Heating              <fct> 1, 1, 1, 1, 1, 1, 1, 1, ...
$ Free_parking         <fct> 0, 0, 0, 0, 0, 0, 0, 0, ...
$ Workspace            <fct> 1, 1, 1, 1, 1, 0, 0, 1, ...
$ Tv                   <fct> 0, 1, 1, 1, 1, 1, 1, 1, ...
$ Kitchen              <fct> 0, 0, 1, 0, 0, 0, 0, 1, ...
$ Washer               <fct> 1, 0, 1, 0, 1, 1, 1, 1, ...
$ Garden               <fct> 1, 0, 0, 0, 0, 0, 0, 0, ...
$ Waterfront           <fct> 0, 0, 0, 0, 0, 0, 0, 0, ...
$ Elevator             <fct> 0, 0, 1, 0, 0, 0, 0, 0, ...
$ Fireplace            <fct> 0, 0, 0, 0, 0, 0, 0, 0, ...
$ Doorman              <fct> 0, 0, 0, 0, 0, 0, 0, 0, ...
$ Balcony              <fct> 0, 0, 0, 0, 0, 0, 0, 0, ...
$ Hot_tub              <fct> 0, 0, 0, 0, 0, 0, 0, 0, ...
$ Pets                 <fct> 0, 0, 0, 0, 0, 0, 1, 0, ...

标签: rdplyraggregatedata-manipulationsummarize

解决方案


尝试这个

library(dplyr)
data %>%
  group_by(Company, City) %>%
  summarize(Total_Price = sum(Price, na.rm = TRUE))


# A tibble: 9 x 3
# Groups:   Company [3]
  Company City      Total_Price
  <fct>   <fct>           <int>
1 ABC     Amsterdam       31245
2 ABC     Berlin          39339
3 ABC     Paris           23913
4 DEF     Amsterdam       55513
5 DEF     Berlin          56290
6 DEF     Paris           30423
7 GHI     Amsterdam       46637
8 GHI     Berlin          21472
9 GHI     Paris           61830

推荐阅读