Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205330

How to groupby dataframe based on a specific column and add the aggregations as a separate column in R?

$
0
0

I've a dataframe that looks like this:

  PM2.5 PM10 SO2 datetime
1     4    4   7  2013-3-1
2     8    4   7  2013-3-1
3     7    7   3  2013-3-1
4     6    6   3  2013-3-2
5     3    3   3  2013-3-2
6     5    5   4  2013-3-2

Now i want to groupby all the columns based on datetime column and after manipulations the resultant dataframe should look like shown below:

       PM2.5       PM10       SO2    datetime   PM2.5_mean  PM10_mean  SO2_mean  PM2.5_min  PM10_min  SO2_min   PM2.5_max  PM10_max  SO2_max
1    [4,8,7]    [4,4,7]   [7,7,3]    2013-3-1         6.33          5      5.66          4         4        3           8         8        7  
2    [6,3,5]    [6,3,5]   [3,3,4]    2013-3-2         4.66       4.66      3.33          3         3        3           6         6        4 

I tried to apply aggregate function, but with that i can only get either mean / min / max. But i want to mutate the mean, min, max as separate columns for each existing column in the dataframe. How can i do it? or Is there any other way by which i can get the required result?


Viewing all articles
Browse latest Browse all 205330

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>