Re: [R] "apply" question

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.ac.be>
Date: Tue 03 May 2005 - 01:13:32 EST

you could try something like this:

dat <- rbind(c(1, 2, 5, 2, 3, NA, 5, NA, 1, 4),

             c(3, 1, 4, 5, 2, 6, NA, 4, NA, 1)) ##########
# (1)
rowSums(is.na(dat[, 6:10]))

## (2)
dat. <- dat[, 1:5]
dat.[is.na(dat[, 6:10])] <- NA
rowSums(dat., na.rm=TRUE)
rowMeans(dat., na.rm=TRUE)

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


> Dear R users,
>
> Iīve got a simple question but somehow I canīt find the solution:
>
> I have a data frame with columns 1-5 containing one set of integer
> values, and columns 6-10 containing another set of integer values.
> Columns 6-10 contain NAīs at some places.
>
> I now want to calculate
> (1) the number of values in each row of columns 6-10 that were NAīs
> (2) the sum of all values on columns 1-5 for which there were no
> missing values in the corresponding cells of columns 6-10.

>
>
> Example: (letīs call the data frame "data")
>
> Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 Col9 Col10
> 1 2 5 2 3 NA 5 NA 1 4
> 3 1 4 5 2 6 NA 4 NA 1
>
> The result would then be (for the first row)
> (1) "There were 2 NAīs in columns 6-10."
> (2) The mean of Columns 1-5 was 2+2+3=7" (because there were NAīs in
> the 1st and 3rd position in rows 6-10)

>
> So far, I know how to calculate the rowSums for the data.frame, but
> I donīt know how to condition these on the values of columns 6-10
>
> rowSums(data[,1:5]) #thatīs straightforward
> apply(data[,6:19],1,function(x)sum(is.na(x))) #this also works fine
>
> But I donīt know how to select just the desired values of columns
> 1-5 (as described above)
>
>
> Can anyone help me? Thanks a lot in advance!
>
> Best regards
> Christoph
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue May 03 01:36:03 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:31 EST