Sunday, 15 May 2011

Error message in R after by or split file processing? -



Error message in R after by or split file processing? -

i run next code in r:

library("aer") data(cps1985,package="aer") by(cps1985[c("wage","age","experience")],cps1985["gender"],mean,na.rm=true)

but whenever error message follows:

by(cps1985[c("wage","age","experience")],cps1985["gender"],mean,na.rm=true) gender: male [1] na gender: female [1] na warning messages: 1: in mean.default(data[x, , drop = false], ...) : argument not numeric or logical: returning na 2: in mean.default(data[x, , drop = false], ...) : argument not numeric or logical: returning na

i have checked before running code wage, age , experience numeric , gender factor variable. little confused why getting error message?

thank you.

you need utilize colmeans by when there more 1 column

by(cps1985[, c("wage", "age", "experience")], cps1985["gender"], fun=colmeans, na.rm=true) #gender: male # wage age experience # 9.994913 35.979239 16.965398 # ------------------------------------------------------------ #gender: female # wage age experience # 7.878857 37.840816 18.832653

or utilize summarise_each dplyr

library(dplyr) cps1985 %>% group_by(gender) %>% summarise_each(funs(mean=mean(., na.rm=true)), wage, age, experience) # source: local info frame [2 x 4] # gender wage age experience # 1 male 9.994913 35.97924 16.96540 # 2 female 7.878857 37.84082 18.83265

r

No comments:

Post a Comment