I have a data.frame with 3 variables and 1.425.558 observations. It´sa register of installed electric power from renewable energy plants. Every row stands for one installed power plant. There can be multiple power plants of the same type in a zipcode-area.
ID zipcode Type power
1 79280 solarpower 3
2 79280 solarpower 3
3 79283 hydroelectric 3
4 79280 biogas 55
5 79280 windpower 2
6 21459 windpower 4
7 21459 windpower 2
I would like to sum by zipcode how much solarpower/biogas/windpower is installed.
zipcode Type power
21459 windpower 6
79280 solarpower 6
79280 windpower 2
...and so on.
I already tried
aggregate(myDat$power, by=list(myDat$zipcode,myDat$type), FUN=sum)
but my RAM was not sufficant.
I know, my dataframe is very big. I could narrow it down a lot, because i only need the data for those zipcodes that start with "2".
Could you point me to a solution? Thank you very much for helping an Beginner!
If I understand correctly what you need, you can express it using dplyr:
> data %.% group_by( zipcode, Type ) %.% summarise( power = sum(power) )
Source: local data frame [5 x 3]
Groups: zipcode
zipcode Type power
1 21459 windpower 6
2 79280 windpower 2
3 79280 biogas 55
4 79283 hydroelectric 3
5 79280 solarpower 6
And if you only want those zip code that start by 2
, you can filter
first:
> data %.% filter( grepl( "^2", zipcode ) ) %.%
group_by( zipcode, Type ) %.% summarise( power = sum(power) )
Source: local data frame [1 x 3]
Groups: zipcode
zipcode Type power
1 21459 windpower 6
data.table
version:
library(data.table)
dt = data.table(your_df)
dt[, sum(power), by = list(zipcode, Type)]
And to narrow down first:
dt[grep("^2", zipcode), sum(power), by = list(zipcode, Type)]
Because grep
is expensive, in both dplyr
and data.table
you're likely better off (speed-wise) summarizing first, and filtering second, ie:
dt[, sum(power), by = list(zipcode, Type)][grep("^2", zipcode)]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.