How to create a column of count/frequency in r

Viewed 36

I have a dataframe which has a series of lengths in it

eg.,

Length

1.2

3.5

3.6

3.6

Some of these lengths are the same

I want to add a column to my dataframe which has the count/frequency of each length - eg.,

Length  Count
 1.2      1
 3.5      1
 3.6      2

Just wondering if anyone knows the most efficient way to do this? Thanks

1 Answers

In base R coud could also do:

df <- data.frame("length"=c(1.2,3.5,3.6,3.6))
df <- as.data.frame(table(df$length))
colnames(df) <- c("length", "count")
Related