Sustract lowest from highest value if they belong to the same group

Hello,
I recently started to explore dyplr and its many functions.
I used the "summarize()" command to come up with the following data frame:
Capture

In a next step I would like to do the following:
1: Identify for each "Genotype" the maximun and the minimun "Mean_PG" value (e.g. this would be 6.47 and 5.22 for Genotype A)

2: Substract the minimum from the maximum value, do this for each genotype (e.g. for A 6.47-5.22, for B 5.96-4.11 ....)

3: create a new data frame including the Genotype columns as well as the results obtained under step 2.

Can someone help me to figure out how I could do that with dyplr? Thanks a lot!
Mike

pass your existing summary data frame to dplyr::group_by and set Genotype as the only group

simply use mutate to create a min column and a max column , min and max are base functions that work comfortably with dplyr.
you can add another entry to your mutate to hold the difference, simply use the minus symbol to subtract one column from the other.

As its all been done on one frame you never lost your other columns so no need to add them back.

good luck.
If you require more specific help then it would be good of you to provide a reprex (for example by dput() some portion of your summary data that you wish to begin transforming further).

1 Like

Dear nirgrahamuk
Thanks for your reply.
Your anser was very useful and helped me a lot. Instead of "mutate" I used "summarize" to get grouped values.

My solution looks like this

dta_geno<-avLn_dta%>%
group_by(Genotype)%>%# only group by genotype!
summarise(
MaxPGbyGeno = max(Mean_PG, na.rm = T),
MinPGbyGeno = min(Mean_PG, na.rm = T),
Diff=MaxPGbyGeno-MinPGbyGeno)

Mike

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.