[R] conditional rowsums in sapply

Assu assu.senra at gmail.com
Mon May 16 16:25:26 CEST 2011


Hi all

I have a data frame with duplicate columns and i want to remove duplicates
by adding rows in each group of duplicates, but have lots of NA's. 
Data:
dfrm <- data.frame(a = 1:4, b= 1:4, cc= 1:4, dd=1:10, ee=1:4)
names(dfrm) <- c("a", "a", "b", "b", "b")
dfrm[3,2:3]<-NA
dfrm
    a  a  b  b  b
1   1  1  1  1  1
2   2  2  2  2  2
3  NA NA NA  3  3
4   4  4  4  4  4
I did: sapply(unique(names(dfrm)),function(x){ 
rowSums(dfrm[ ,grep(x, names(dfrm)),drop=FALSE])})
 which works. However, I want rowSums conditional:
1) if there is at least one value non NA in a row of each group of
duplicates, apply rowSums to get the value independently of the existence of
other NA's in the group row.
2) if all values in a row of duplicates are NA, I get NA
In my data dfrm I would get

     a   b
1    2   3
2    4   6
3   NA  6
4    8  12
Can't use na.rm=TRUE or FALSE.
I tried: sapply(unique(names(dfrm)),function(x) ifelse(any(!is.na(dfrm[
,grep(x, names(dfrm))])), rowSums(dfrm[ ,grep(x,
names(dfrm)),drop=FALSE],na.rm=TRUE),NA))

and it doesn't work.
Can someone please help me?
Thanks in advance.







--
View this message in context: http://r.789695.n4.nabble.com/conditional-rowsums-in-sapply-tp3526332p3526332.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list