0

I am currently working on my masters thesis and part of my data analysis is in R. I am completely new to it and so am learning as I go along.

The experiments we are running consist of individuals playing a token allocation game, over a series of rounds.

I need to change the current csv file in R so that each individual appears in one row, with ingroup, outgroup and self giving summed over the 40 rounds they played.

Currently, the data frame is as follows:

id       roundno        tokenstoingroup    tokenstooutgroup     tokenstoself
0001        1                 1                   0                  0
0001        2                 0                   1                  0
0002        1                 0                   0                  1

etc...

There are many participants (over a thousand), and every round's allocation for each participant is entered.

My question is:

How do I sum this up so that the data frame looks more like this??

id      totalrounds  tokenstoingroup      tokenstooutgroup   tokenstoself
0001       40             25                13                     2

002        40             13                13                     14

etc...

As I have said, I am totally new to this. I have tried to look online for aggregating and summing things up, but I have idea where to start with something a bit more complex like this.

KirstyK
  • 3
  • 2

2 Answers2

2

You can use the aggregate function with cbind. As an example, let's create a data frame:

test <- data.frame('id'=rep(c('A','B','C'),each=2),'C1'=rep(1,6),'C2'=1:6)
> test
     id C1 C2
   1  A  1  1
   2  A  1  2
   3  B  1  3
   4  B  1  4
   5  C  1  5
   6  C  1  6

Then:

test <- aggregate(cbind(C1,C2)~id,data=test,sum)
> test
   id C1 C2
1  A  2  3
2  B  2  7
3  C  2 11
Pierre Dudek
  • 252
  • 4
  • 11
0

We can use summarise_each from dplyr

library(dplyr)
df1 %>%
  group_by(id) %>% 
  summarise_each(funs(sum), roundno, tokenstoingroup,tokenstooutgroup,   tokenstoself)
akrun
  • 874,273
  • 37
  • 540
  • 662