简体   繁体   中英

How to create a column of count/frequency in r

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

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM