0

This is my first post to stack overflow, so let me know if I am doing this wrong!

Trying to do this in R studio.

I have a data frame where I want to sum the size_adjusted column based on a grouping from the Row, Col, and Prion columns only if the Image_number value is > 250:

See my dataframe, (Sorry couldn't figure out how to format it nicely in the message

I have used the following successfully (without the condition in the Image_number column).

Threshold <- mydataframe %>% group_by(Row, Col, Prion) %>% summarise(AUC, sum(size_adjusted))

How do I make the condition? I have tried to use the "if" function, but receive errors.

Thank you all!

Best,

Alex

  • 1
    do you want to keep the data when value is < 250 or no? if not then you can use `filter` to remove them before summarizing. – novica Aug 23 '20 at 18:04
  • Ypu can filter by that condition: `mydataframe %>% filter(Image_number > 250) %>% etc`. – Rui Barradas Aug 23 '20 at 18:06
  • Hi, read our help page to figure out how to nicely provide and format data: [how-to-make-a-great-r-reproducible-example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – jay.sf Aug 23 '20 at 19:29

1 Answers1

0

There are multipe ways of doing it, here is something that should be quite easy, we crate a logical vector like so:

condition = mydataframe$Image_number >250

next we simply insert our condition vector as if it was an index:

mydataframe[condition,] %>% 
  group_by(Row, Col, Prion) %>% 
    summarise(AUC, sum(size_adjusted))

it is also possible to just avoid the first step and write your condition inside:

    mydataframe[mydataframe$Image_number >250,] %>% 
     group_by(Row, Col, Prion) %>% 
      summarise(AUC, sum(size_adjusted))
dvd280
  • 876
  • 6
  • 11