Hi Jesse,

Here is one approach:

score <- function(dat, minimumN) {
  # get the number of columns (variables)
  k <- ncol(dat)
  # take the row means, excluding missing
  mean <- rowMeans(dat, na.rm=TRUE)
  # get the number missing for each row
  nmiss <- rowSums(is.na(dat))

  # if nmiss is greater than threshold, set to missing
  # k = columns, minimum N is
  mean[nmiss > (k - minimumN)] <- NA

  # calculate the sum (reweighted by missing)
  sum <- mean * k

  # put results in a dataframe and return
  data.frame(Mean = mean, Sum = sum, Nmissing = nmiss)
}

# score 4 variables, requiring at least 3
score(mtcars[, c("mpg", "disp", "hp", "wt")], 3)

# or put back into dataset, just the mean

mtcars$ScaleMean <- score(mtcars[, c("mpg", "disp", "hp", "wt")], 3)$Mean

It will be somewhat faster than using apply() because all you need are
rowMeans, which uses more optimized code.

Cheers,

Josh




On Thu, Oct 3, 2013 at 3:42 PM, Jesse Gervais <jgervai...@gmail.com> wrote:

> Hello there,
>
> I try to construct a variable with R, but I have some difficulties.
>
> Assume that I use a data set named = mydata. I want to create a variable
> that is the mean (totmean) or the sum (totsum) of 6 variables (var1, var2,
> var3, var4, var5, var6). However, I want only participants who have
> responded to at least 4 items to be include. Those who have one or two
> missing for var1-var6 should be coded NA for totmean and totsum.
>
> How I do that?
>
> Thank you!
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://joshuawiley.com/
Senior Analyst - Elkhart Group Ltd.
http://elkhartgroup.com

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to