For this problem I am trying to examine a column within the dataset data called firstdigits (it is the 22nd column), determine how many times each value occurs, and put that into a new column called count (the 27th column). So say that a 1 occurs in data$firstdigits a total of 5 times, everywhere where data$firstdigits=1, I want data$count=5 in that row.
The method that I've come up with might work but its so clunky that it hasn't finished running yet for me to know. I'm looking for a faster way to achieve this.
unique = as.data.frame(unique(data$firstdigits))
count = as.data.frame(0)
for (i in 1:nrow(unique)){
count[i,1] = sum(data$firstdigits == unique[i,1])
}
data$count = 0
for(j in 1:nrow(data)){
for(k in 1:nrow(unique)){
if (data[j,22] == unique[k,1]){
data[j,27] == count[k,1]
}
}
}