La Machine Infernale
La Machine Infernale

Reputation: 569

R: append column in a dataframe with frequency count based on two columns

I have a dataset like that:

name  height 
John    188
John    190
Jack    182
Jack    174
Jack    174

And I need to append a column with the frequency count based on both (name) and (height), without modifying the structure of my dataset, e.g. like so:

name  height occurrence
John    188      1
John    190      1
Jack    182      1
Jack    174      2
Jack    174      2

Is there a one liner to do so? I've looked around but don't seem to find an answer to that. Thank you

Upvotes: 2

Views: 1683

Answers (3)

count
count

Reputation: 1338

You could also use dplyr:

require(dplyr)
data %>% 
   group_by(name,height) %>% 
   mutate(new = n()) %>% 
   data.frame()

Upvotes: 2

Rentrop
Rentrop

Reputation: 21497

Using data.table you can use (Thanks @David Arenburg for the improvement)

require(data.table)
setDT(df1)[, occurrence := .N, by = .(name, height)]

My original answer (which copies the data insted of adding it by reference via :=)

require(data.table)
setDT(df1)[,rep(.N,.N),.(name, height)]

Upvotes: 5

Ronak Shah
Ronak Shah

Reputation: 388817

You can use base R ave function like

ave(1:nrow(df), paste(df$name, df$height), FUN = length)
# [1] 1 1 1 2 2

Upvotes: 1

Related Questions