Sum values in a column based on values in other columns

Hello all,

This is my first query in the RStudio Community, so please tell me if I am doing this wrong.

From this data frame (see subset below), I want to add the "size"(s) of all rows with the same "Row" "Col" identifiers. This is a large data frame, where there are 1536 different "Row" and "Col" positions, with size measurements across 800-1000 time points. More specifically there are 1-32 "Row" identifiers and 1-48 "Col" identifiers, every combinatorial pair resulting in 1536 different combinations.

Ideally, I would like to define a script to loop summing for the different "Row" and "Column" identifiers, and save the output of size sums with the "Row" and "Column" labels in a new csv. I am super green to R, so any pushes in the right direction are appreciated!!

My Data Frame:

  Plate Image_number Row Col size circularity flags                        Prion
1    1a          129   1   1  110      0.9982                 POL32-1856_x129_A1
2    1a          129   1   2  116      0.9598                 POL32-1856_x129_A1
3    1a          129   1   3  109      0.8648       SAP1orSMP1-1860or733_xDEL_A1
4    1a          129   1   4  105      0.8955       SAP1orSMP1-1860or733_xDEL_A1
5    1a          129   1   5  100      0.9492                 POL32-1856_x129_D1
6    1a          129   1   6  102      0.9682                 POL32-1856_x129_D1
> 

Hopeful output matrix:

 Size_Sum Row Col Prion
1        0   0   0     0
2        0   0   0     0
3        0   0   0     0
4        0   0   0     0
5        0   0   0     0
6        0   0   0     0
...
1536  0  0  0  0

I thank you all for your time and consideration

Yep you can use group_by and summarise in dplyr. It requires that Prion only has one value in each Row, Cow group, otherwise you will need to summarise Prion as well. It will only give rows for Row, Col pairs that are in the dataset.

library(dplyr)

newcsv <- mydataframe %>%
  group_by(Row, Col, Prion) %>%
  summarise(
    Size_Sum = sum(size)
  )
1 Like

I hope this example will work.

row <- c(1, 1, 2)
size <- c(10, 15, 10)
df=as.data.frame(row,size)
result  <- aggregate(size ~ row, df, sum)
1 Like

Thanks so much!! It worked!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.