R error in unique.default(x) unique() applies only to vectors -


i created dataset named state built-in matrix state.x77 2 continuous variables (population , income) , 2 factor variables (region , area).

i computed mean income region using tapply(), by(), aggregate(), , ave() see format of returned object.

but call ave() giving error

error in unique.default(x) : unique() applies vectors

the code is:

## mean income region tapply(state$inc, state$region, mean) # northeast         south north central          west  # 4570.222       4011.938      4611.083      4702.615   by(state$inc, state$region, mean) # state$region: northeast # # [1] 4570.222 # [...]  aggregate(state$inc, list(state$region), mean) # #         group.1        x # 1     northeast 4570.222 # 2         south 4011.938 # 3 north central 4611.083 # 4          west 4702.615  ave(state$inc, state$region, mean) # error in unique.default(x) : unique() applies vectors 

why error occurring? how can prevent it?

this common mistake, need use named argument fun:

ave(state$inc, state$region, fun = mean) 

otherwise mean interpreted grouping variable (part of ... argument ave.)


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -