Add column with counts of another [duplicate]
Solution 1:
You may try ave
:
# first, convert 'gender' to class character
df$gender <- as.character(df$gender)
df$count <- as.numeric(ave(df$gender, df$gender, FUN = length))
df
# gender age count
# 1 m 18 4
# 2 f 14 2
# 3 m 18 4
# 4 m 18 4
# 5 m 15 4
# 6 f 15 2
Update following @flodel's comment - thanks!
df <- transform(df, count = ave(age, gender, FUN = length))
Solution 2:
Since gender
is a factor, you can use it to index the table
output:
dat$count <- table(dat$gender)[dat$gender]
Or to avoid repeating dat$
too many times:
dat <- transform(dat, count = table(gender)[gender])
Solution 3:
Using plyr
:
library(plyr)
ddply(dat,.(gender),transform,count=length(age))
gender age count
1 f 14 2
2 f 15 2
3 m 18 4
4 m 18 4
5 m 18 4
6 m 15 4
Solution 4:
And a data.table
version for good measure.
library(data.table)
df <- as.data.table(df)
Once you have the data.table
, it's then a simple operation:
df[,count := .N,by="gender"]
df
# gender age count
#1: m 18 4
#2: f 14 2
#3: m 18 4
#4: m 18 4
#5: m 15 4
#6: f 15 2