-1

I am trying to normalize a Y scale by converting all values to percentages. Therefore, I need to divide every number in a column by the first number in that column. In Excel, this would be equivalent to locking a cell A1/$A1, B1/$A1, C1/$A1 then D1/$D1, E1/$D1...

The data needs to first meet four criteria (Time, Treatment, Concentration and Type) and the reference value changes at every new treatment. Each treatment has 4 concentrations (0, 0.1, 2 and 50). I would like for the values associated to each concentration to be divided by the reference value (when the concentration is equal to 0).

The tricky part is that this reference value changes every 4 rows.

I have tried doing this using ddply:

`MasterTable <- read.csv("~/Dropbox/Master-table.csv")`
  MasterTable <- ddply(MasterTable, .(Time, Type, Treatment), transform, pc=(Value/Value$Concentration==0)) 

But this is not working at all. Any help would be really appreciated!

My data file can be found here: Master-table

Thank you!

Marty999
  • 213
  • 1
  • 4
  • 12
  • Possible duplicate of [In R, how do I compute factors' percentage given on different variable?](https://stackoverflow.com/questions/24901061/in-r-how-do-i-compute-factors-percentage-given-on-different-variable) – Jaffer Wilson Jul 26 '17 at 04:40

1 Answers1

1

dplyr is very efficient here:

library(dplyr)
result <- group_by(MasterTable, Time, Type, Treatment) %>%
  mutate(pc = Value / Value[1])
Remko Duursma
  • 2,741
  • 17
  • 24
  • Thank you Remko! You figured it out, its exactly what I was looking for. – Marty999 Jul 27 '17 at 01:16
  • Hello Remko, the above solution you provided to me last year is no longer working... it always divides the number by the first value in the column. It's as if the grouping function is being ignored. Has there been a change to dplyr that would cause this? Any help you could provide me would be greatly appreciated. – Marty999 Aug 09 '18 at 15:32
  • I will take a look. No surprise that dplyr is not backward compatible. – Remko Duursma Aug 18 '18 at 19:02