i feel should easy, have looked x internet, keep getting error messages. have done plenty of analytics in past new r , programming.
i have pretty basic function calculate means x columns of data:
columnmean <-function(y){ nc <- ncol(y) means <- numeric(nc) for(i in 1:nc) { means[i] <- mean(y[,i]) } means }
i'm in rstudio , testing using included 'airquality' dataset. when load aq dataset , run function:
data("airquality") columnmean(airquality)
i back:
na na 9.957516 77.882353 6.993464 15.803922
because first 2 variables in aq have nas in them. k, cool. want suppress nas such r ignore them , run function anyway.
i reading can specify na.rm=true, like:
columnmean(airquality, na.rm = true)
but when this, error message saying:
"error in columnmean(airquality, na.rm = true) : unused argument (na.rm = true)"
i'm reading on place need include na.rm = true , function run , ignore na values...but keep getting error. have tried use = "complete" , else can find.
two caveats:
i know can create vector is.na , subset data, don't want step, want run function , ignore missing data.
i know can specify in function ignore or not ignore, i'd way choose ignore/not ignore on fly, on action action basis, rather having part of function itself.
help appreciated. thank you, everyone.
we can include na.rm = true
in mean
columnmean <-function(y){ nc <- ncol(y) means <- numeric(nc) for(i in 1:nc) { means[i] <- mean(y[,i], na.rm = true) } means }
if need use na.rm
argument false , other times true, specify in argument of 'columnmean'
columnmean <-function(y, ...){ nc <- ncol(y) means <- numeric(nc) for(i in 1:nc) { means[i] <- mean(y[,i], ...) } means } columnmean(df1, na.rm = true) #[1] 1.5000000 0.3333333 columnmean(df1, na.rm = false) #[1] 1.5 na
data
df1 <- structure(list(num = c(1l, 1l, 2l, 2l), x1 = c(1l, na, 0l, 0l )), .names = c("num", "x1"), row.names = c(na, -4l), class = "data.frame")
Comments
Post a Comment