Search code examples
rdplyraggregatedata-manipulationsummarize

Summing Up A Certain Column in 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
    ...................

Sorry guys, I am quite new in R. Could you tell me how to do a sum if? I would like to calculate the total revenue of each company in each city. For example, in Berlin, company GHI makes 13000 Euro per month. company DEF makes 22000 Euro per month, company ABC makes 8000 Euro per month, in Amsterdam, ............

Sorry for my previous mistake!

Here is a glimpse of the data.

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, ...

Solution

  • Try this

    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