0

I am discovering R and I need to do some "grouping by" functionnality so I tried this

test<-data.frame(A=c(1,2,3,4),B=c(4,4,5,6))
test[,by=list(B)]

and I got this

Error in `[.data.frame`(test, , by = list(B)) : 
  unused argument (by = list(B))

where am I going wrong ?

user2478159
  • 133
  • 1
  • 14

3 Answers3

0

Might want to look into the packages created by Hadley Wickham, starting with dplyr which is a part of the tidyverse for data wrangling. Also here are two simple examples of what you could do after the grouping.

library(dplyr)
test<-data.frame(A=c(1,2,3,4),B=c(4,4,5,6))
test %>% 
  group_by(B) %>% 
  summarise(counts = n(),
            sum = sum(A))

# A tibble: 3 x 3
      B counts   sum
  <dbl>  <int> <dbl>
1     4      2     3
2     5      1     3
3     6      1     4

He has written a couple books and has a nice website that explains the packages.

Geochem B
  • 418
  • 3
  • 13
0

Add one more row convert data.frame to data.table

test<-data.frame(A=c(1,2,3,4),B=c(4,4,5,6))
test=data.table(test)
test[,by=list(B)]


   A B
1: 1 4
2: 2 4
3: 3 5
4: 4 6
BENY
  • 317,841
  • 20
  • 164
  • 234
  • you mean we have to convert a `data.frame` into `data.table why so ? – user2478159 Jul 20 '17 at 15:15
  • `test[,by=list(B)]`, this is `data.table` function not for `data.frame`, `data.frame` and `data.table` are different data Structure. – BENY Jul 20 '17 at 15:18
0

If you just want to group and sum you can use:

aggregate(test$A, by=list(test$B), FUN = sum)

Olivia
  • 814
  • 1
  • 14
  • 26