I am new to the R programming, so wanted to learn that if it is possible to perform merging and grouping of the data with a single function or within a single step in R.
Asked
Active
Viewed 2,972 times
0
-
1I am not 100% sure if I understand the question correctly, but I'm always 100% happy to suggest to give `data.table` a try :) – daroczig Mar 27 '15 at 07:42
-
there should be *join vignette* in some time, for now you can find that feature listed by Arun (one of two main data.table devs) [here](http://stackoverflow.com/a/27718317/2490497). See `3. Syntax -> 3. joins -> 3. aggregate while join`. Feel free to answer your question and close it as answered. – jangorecki Mar 27 '15 at 07:47
-
If you know SQL you can always install the sqldf package which allows you to use SQL and apply them to data frames. For example `library(sqldf); newdata <- sqldf('select * from data1 inner join data2 group by column')` – JoeArtisan Mar 27 '15 at 08:39
1 Answers
0
I'm not sure if I've understood your question correctly. It's possible to group and merge data via the aggregate function:
df <- data.frame(a=1:40, b=rbinom(40, 10, 0.5), n=rnorm(40), p=rpois(40, lambda=4), group=gl(4,10), even=rep(c(1,2),20))
require(plyr)
aggregate(b ~ group, df, sum) #aggregate/sum over group
aggregate(b ~ group + even, df, sum) #aggregate/sum over group & even
Results:
> aggregate(b ~ group, df, sum)
group b
1 1 51
2 2 49
3 3 49
4 4 47
> aggregate(b ~ group + even, df, sum)
group even b
1 1 1 27
2 2 1 23
3 3 1 25
4 4 1 23
5 1 2 24
6 2 2 26
7 3 2 24
8 4 2 24

nathanielng
- 1,645
- 1
- 19
- 30