-1

My df has three variables. NSCore , NS_Group (which is categorical, have three levels Low,High,Very High). SOC(Which is categorical having three levels SOC1,SOC2 and SOC3). Looking like that.

df
NScore   NS_group   SOC
1      Low      SOC1
2      Low      SOC2
3      High     SOC3
4      High     SOC2
5      Very High SOC3

I am using following code in R But this is giving me frequency of NScore against NS_group and SOC . But i want to see the mean of NScore against these two.

 `tab1 <- with(df, table(df$NS_group,df$SOC))

I want MEAN OF NScore (Not the frequency) against these two categories something as follows.

          SOC1  SOC2  SOC3
Low        1     1     2
High       1     2     1
VeryHigh   1     1     2

p.s All the values are hypothtical just to explain what i am asking

Aryh
  • 479
  • 1
  • 4
  • 16

1 Answers1

0

If you want a base R solution use this:

aggregate(SOC ~ NS_group, df, mean)
Claudiu Papasteri
  • 2,469
  • 1
  • 17
  • 30
  • unfortunately it is not working as both SOC and NS_group are categorical. I would like to ass that NS_group is actually based on NScore. Like people having NScore 0-2 are categorized as Low. – Aryh Mar 24 '20 at 12:35
  • well just add `as.numeric(SOC)` because means of (unordered) factors don't really make sense – Claudiu Papasteri Mar 24 '20 at 15:00