[R] ave and levels

Patrick Hausmann c18g at uni-bremen.de
Mon Nov 19 12:10:18 CET 2007


Dear list,

I want to calculate the standard deviation using
'ave' on two different DFs.

In the first DF M1 has only 1 level:

> str(x)
'data.frame':   18 obs. of  3 variables:
  $ M1: Factor w/ 1 level "A03": 1 1 1 1 ...
  $ M2: num  2.76 2.93 3.06 3.07 3.12 ...
  $ M3: Factor w/ 2 levels "Ausgewählt","Nicht ausgewählt": 1 1 1 1 ...

and I am getting a correct 'NA' for the last value
ave(x$M2, x$M1, factor(x$M3), FUN = sd)
# [1] 0.1810123 0.1810123 0.1810123 0.1810123
# 0.1810123 0.1810123 0.1810123 0.1810123 0.1810123
# 0.1810123 0.1810123 0.1810123 0.1810123
# 0.1810123 0.1810123 0.1810123 0.1810123        NA

This ist the second DF (here M1 as 138 Levels):

> str(k)
'data.frame':   18 obs. of  3 variables:
  $ M1: Factor w/ 138 levels "A01","A02","A03",..: 3 3 3 3 ...
  $ M2: num  2.76 2.93 3.06 3.07 3.12 3.12 3.15 3.17 3.17 3.17 ...
  $ M3: Factor w/ 2 levels "Ausgewählt","Nicht ausgewählt": 1 1 1 1 ...

and I am getting this error
ave(k$M2, k$M1, factor(k$M3), FUN = sd)
#Fehler in var(x, na.rm = na.rm) : 'x' ist leer

So, what have I missed?
Thank you for any help!

Patrick



More information about the R-help mailing list