0

I have a data frame where one column is string names (v1), and the second column is string values(v2), I would like to aggregate those entries and sum their string values.(V1 and V2 are list)

V1 and V2 are list

trp_str

    V1  V2

1 a,x,v 0.07128713

2  b,x,c  0.06336634

3  c,x,v  0.04752475

4 c,x,v  0.04752475

5 d,x,v  0.06336634

6 a,x,v 0.07128713

i want to get like this
  V1     V2
1 a,x,v 0.14257426

2  b,x,c  0.06336634

3  c,x,v  0.0950495

4  d,x,v  0.06336634

when i used aggregate(trp_str$V1, by=list(V2=trp_str$V2), FUN=sum) following error occured Error in aggregate.data.frame(as.data.frame(x), ...) : arguments must have same length

Kiran Pg
  • 43
  • 9
  • 1
    dput() of sample data – sai saran Nov 19 '18 at 06:57
  • u can directly use aggregate function right? – sai saran Nov 19 '18 at 06:58
  • Pretty sure there are lot of answers for this question already, but you want `aggregate`. Or the `dplyr` package (`group_by` + `summarise`) or the built-in aggregation capabilities of the `data.table` package – arvi1000 Nov 19 '18 at 07:00
  • when i used aggregate(trp_str$V1, by=list(V2=trp_str$V2), FUN=sum) following error occured Error in aggregate.data.frame(as.data.frame(x), ...) : arguments must have same length – Kiran Pg Nov 19 '18 at 07:09
  • @KiranPg provide reproducible example. I think you need `aggregate(trp_str$V2, by=list(V1=trp_str$V1), FUN=sum)` – zx8754 Nov 19 '18 at 07:30
  • aggregate(trp_str$V2, by=list(V1=trp_str$V1), FUN=sum) it show Error in aggregate.data.frame(as.data.frame(x), ...) : arguments must have same length – Kiran Pg Nov 19 '18 at 07:32

1 Answers1

0

This is very simple operation. A lot of it already has been answered on this site

This should work for you

library(dplyr)

df <- data.frame(V1 = c('a,x,v','b,x,c', 'c,x,v', 'c,x,v', 'd,x,v', 'a,x,v'), 
                 V2 = c(0.07128713, 0.06336634, 0.04752475, 0.04752475, 0.06336634,0.07128713))

df %>% group_by(V1) %>% summarise(Total = sum(V2)) 

Basically, you group by V1 column, then use the summarise function to sum up the values in V2 column. Here, I named the column that holds the sums Total but you can name it whatever you want.

Wally Ali
  • 2,500
  • 1
  • 13
  • 20
  • when i apply this show Error in grouped_df_impl(data, unname(vars), drop) : Column `V1` can't be used as a grouping variable because it's a list – Kiran Pg Nov 19 '18 at 07:46
  • if you have your data in a list format, convert it to a dataframe. I thought you said you have a dataframe. – Wally Ali Nov 20 '18 at 05:11