-1

in the data Arthritis of package 'vcd', after grouping by Treatment and Sex, i would like to get the no. and percentage of observations in every level (None, Some, Marked) of Improved. how to do it?

Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41
汪燕敏
  • 23
  • 3
  • i have solved part of problem. my code is : Arthritis %>% group_by(Treatment,Sex ) %>% summarise(no =table(Improved)) – 汪燕敏 Mar 31 '22 at 21:58

2 Answers2

0

"count() lets you quickly count the unique values of one or more variables: df %>% count(a, b) is roughly equivalent to df %>% group_by(a, b) %>% summarise(n = n()) . count() is paired with tally() , a lower-level helper that is equivalent to df %>% summarise(n = n()) ."

PeteRlearner
  • 154
  • 2
  • 7
0

I am not sure if there is a straightforward way to do this, but I would do this:

library(dplyr)
df2<-df %>% group_by(Treatment, Sex) %>% count()
df2$percent<-df2$n*nrow(df)/100
juandelsur
  • 793
  • 7
  • 6