r - compress rows with NA in different columns to create a single row -


i have data frame this

test <- data.frame(id = rep(letters[1:2],each = 3),      = 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 dataframe, , want convert there 1 row each unique 'id' , no nas in dataframe.

i doing this

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

to data.frame

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

it works, there more direct way of doing without using colsums, sort of compress rows create single row each 'id', because within each 'id', columns have 1 value , rest nas. did come across similar request somewhere while looking else cannot find now!

thanks

using r base functions

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

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -