compress lines with NA in different columns to create a single line

advertisements

I have a data frame like this

test <- data.frame(id = rep(LETTERS[1:2],each = 3),
    a = c(1,NA,NA,10,NA,NA),
    b = c(2,NA,NA,20,NA,NA),
    c = c(NA,3,NA,NA,30,NA),
    d = c(NA,NA,4,NA,NA,40))

I got this dataframe, and want to convert it so there only one row for each unique 'id' and no NAs in the dataframe.

I am doing this

ddply(test,
    .variables = 'id',
    .fun = function(df){
        colSums(df[,1:4], na.rm = T)})

to get this data.frame

      id  a  b  c  d
    1  A  1  2  3  4
    2  B 10 20 30 40

It works, but is there a more direct way of doing it without using colSums, sort of compress the rows to create a single row for each 'id', because within each 'id', all columns have only one value and the rest are NAs. I did come across a similar request somewhere while looking for something else but cannot find it now!

Thanks


Using R base functions

> test[is.na(test)] <-0
> aggregate(.~id, data=test, FUN="sum")
  id  a  b  c  d
1  A  1  2  3  4
2  B 10 20 30 40