I have a data frame in rstudio that is like the following example
address = c("123 fake st", "124 fake st", "125 fake st", "126 fake st", "123 jerry st", "124 road rd",
" 125 tiny rd"," 126 cool r")
name = c("joey", "rachel", "ross", "chandler", "monika", "joey", "ross", "ross")
other = c(1, 1, 1, 2, 2, 3, 4, 4)
df <-data.frame(address,name, other
)
This represents a dataset that is basically a series of addresses, the owner names, and a bunch of other columns I need to keep. I want to county how many times the owner shows up, in it's own column, so that it looks like this:
address name other count
1 123 fake st joey 1 2
2 124 fake st rachel 1 1
3 125 fake st ross 1 3
4 126 fake st chandler 2 1
5 123 jerry st monika 2 1
6 124 road rd joey 3 2
7 125 tiny rd ross 4 3
8 126 cool r ross 4 3
Based on some other solutions, I tried this and got the following error. Any advice?
df$count <- group_size(group_by(df,name))
Error in `$<-.data.frame`(`*tmp*`, count, value = c(1L, 2L, 1L, 1L, 3L :
replacement has 5 rows, data has 8
I thought maybe it was because if there wasn't a duplicate then it didn't have anything to put in the count column for the value and tried an (extremely inelegant) ifelse, and got the same problem:
> df$count <- ifelse((group_size(group_by(df,name))), (group_size(group_by(df,name))), 1)
Error in `$<-.data.frame`(`*tmp*`, count, value = c(1L, 2L, 1L, 1L, 3L :
replacement has 5 rows, data has 8
Any advice?