2

My data looks like this:

browsers.id browsers.label browsers.count browsers.id.1 browsers.label.1 browsers.count.1 
OS;FBSV        OS;FBSV            6        Chrome           Chrome                3 
Chrome         Chrome             86       Safari           Safari                47 
Chrome         Chrome             21       OS;FBSV          OS;FBSV               14 

I want to aggregate or merge data to:

browsers.id count
Chrome       110
OS;FBSV      20
Safari       47

I try to use aggregate function,

aggregate(data[ ,3], list(data$browser.id), sum)

But it occurs error:

"arguments must have same length"

Could I fix it? Thanks.

1 Answer 1

1

We subset the 'id', and 'count' columns, convert the 'data.frame' to 'data.table' (setDT(..), reshape the 'wide' format to 'long' using melt (the data.table melt can take multiple measure columns), grouped by 'browsers.id', we get the sum of 'count'.

library(data.table) 
 melt(setDT(df1[c(1,3,4,6)]), measure=patterns('id', 'count'), 
   value.name=c('browsers.id','count'))[,list(count=sum(count)) ,
         by = browsers.id]
 #   browsers.id count
 #1:     OS;FBSV    20
 #2:      Chrome   110
 #3:      Safari    47

Or another option would be to rbind the 'id', 'count' columns

  rbindlist(list(df1[c(1,3)], df1[c(4,6)]))[,  
           list(count= sum(browsers.count)) ,browsers.id]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Because I have more than six column, I try "setDT(df1)" and it works :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.