I want to create a summary of the proportions of 1's and 2's in the groups of
the following data. I'm referring to the column using nme as I will be
using this within a loop
Data:
df <- data.frame(
  x = sample(1:2,100,replace=T),
  g = c( rep(1,20), rep(2,20), rep(3,20), rep(4,20), rep(5,20))
)
First attempt
loop_value <- 1
nme <- names(df)[loop_value]
df %>% 
  group_by(g) %>%
  select(nme, g) %>%
  summarise(s1 = sum(nme==1), 
                p1 = sum(nme==1)/length(nme)
                )
Second attempt
loop_value <- 1
nme <- names(df)[loop_value]
df %>% 
  group_by(g) %>%
  select(nme, g) %>%
  summarise(s1 = sum(df[nme]==1), 
                p1 = sum(df[nme]==1)/length(df[nme]))
Output from first attempt
# A tibble: 5 x 3
      g    s1    p1
  <dbl> <int> <dbl>
1     1     0     0
2     2     0     0
3     3     0     0
4     4     0     0
5     5     0     0
And from second
# A tibble: 5 x 3
      g    s1    p1
  <dbl> <int> <dbl>
1     1    58    58
2     2    58    58
3     3    58    58
4     4    58    58
5     5    58    58
For p1 in group 1 I would expect 0.65 based on 
> prop.table(table(df[df$g==1,]$x))
   1    2 
0.65 0.35 
 
     
    