1

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.

ramiro
  • 125
  • 6

2 Answers2

0

We can use data.table

library(data.table)
setDT(df)[, freq:= val/sum(val) , by = fac2]
df
#   fac1 fac2 val      freq
#1:    a    x  10 0.1666667
#2:    b    x  20 0.3333333
#3:    c    x  30 0.5000000
#4:    a    y  40 0.2666667
#5:    b    y  50 0.3333333
#6:    c    y  60 0.4000000
#7:    a    z  70 0.2916667
#8:    b    z  80 0.3333333
#9:    c    z  90 0.3750000

Or using base R

df$freq <- with(df, val/ave(val, fac2, FUN=sum))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

With library dpyr, you can try something like this:

library(dplyr)
df %>%
  group_by(fac2) %>%
  mutate(freqs = val / sum(val))

Source: local data frame [9 x 4]
Groups: fac2 [3]

    fac1   fac2   val     freqs
  <fctr> <fctr> <dbl>     <dbl>
1      a      x    10 0.1666667
2      b      x    20 0.3333333
3      c      x    30 0.5000000
4      a      y    40 0.2666667
5      b      y    50 0.3333333
6      c      y    60 0.4000000
7      a      z    70 0.2916667
8      b      z    80 0.3333333
9      c      z    90 0.3750000

Is this what you want?

Gopala
  • 10,363
  • 7
  • 45
  • 77
  • thanks @Gopala, this was very useful. just to note that after running this code and if you want a data frame, you then need to do `df<-as.data.frame(df)` – ramiro May 04 '16 at 20:19