簡體   English   中英

總結R中某列

[英]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
    ...................

對不起,我是 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, ...

嘗試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM