This is a question about the utilization of apply. It is purely coincidental that a question of this nature has been answered. I am not looking for the easiest way to do it (which would have made it a duplicate). I wrote something random that can be solved using a loop. I am looking for how my problem can be written using apply.
In R, I'm having a really hard time getting out of the habit of always using loops. Here is a basic sample script I wrote. It takes the data and states the frequency of each unique number in the table.How can I accomplish the same thing with apply, if possible?
x <- c(1,3,4,7,8,10,1,2,3,4,4,6,7,8,8,1,2,3,7,8)
y <- data.frame(x)
z <- data.frame(unique(y)) #finds unique values
i <- nrow(z) #nrow for loop length
id <- 1:i
repped <- data.frame()
for (i in id){
zz <- y[which(y[,1] == z[i,]),] #takes each value from z and finds rows with identical values
value <- length(zz)
repped <- rbind(repped, value)
}
yy <- data.frame(z, repped)
colnames(yy) <- c("number", "frequency")