-1

I have the following data frame:

enter image description here

Some background:

VAR1 is the Unique code, VAR2 is Person ID and FREQ is Total Count.

For example, Person 1 (P1) appears 29 times in code ABCD, they also appear 98 times in code EFGH. What I am trying to do, is make it so it looks like this:

enter image description here

Basically, I just want the total, so Person 1 has used a specific code (VAR1) 127 times. Person 2 has used a unique code 14 times

I'm unsure how to get this to work, as I'm just starting with R.

989
  • 12,579
  • 5
  • 31
  • 53
KieranLowe
  • 81
  • 1
  • 10

3 Answers3

3

In base R (where df is your data frame):

aggregate(.~var2, df, sum)[,c(1,3)]

#  var2 freq
#1   p1  127
#2   p2   14
#3   p3  135
989
  • 12,579
  • 5
  • 31
  • 53
2

Using dplyr we can do this. Assuming df is the name of the data frame:

library("dplyr")    
df %>% group_by(VAR2) %>% summarise(count = sum(VAR3))
svenhalvorson
  • 1,090
  • 8
  • 21
0

A data.table solution:

library(data.table)
dt[, list(freq = sum(Freq)), by = Var2]

#   Var2 freq
#1:   P1  127
#2:   P2   14
#3:   P3  135

Data:

dt <- fread("Var1 Var2 Freq
            ABCD P1 29
            ABCD P2 2
            ABCD P3 12
            EFGH P1 98
            EFGH P2 12
            EFGH P3 123")
Mike H.
  • 13,960
  • 2
  • 29
  • 39