I have a data frame like this:
df<-data.frame(fac1=rep(c("a","b","c"),3),fac2=c(rep("x",3),rep("y",3),rep("z",3)),val=seq(10,90,10))
What I would like to do is to calculate the frequency for each level of fac1 within a given fac2.
I have not used "for loops" much but I thought something like this would do it:
df$freqs<-for(i in unique(df$fac2)) df$val/sum(df$val)
However, nothing gets returned when I do this. If anyone has any idea, that would be great.