Marty999 Marty999 - 1 year ago 70
R Question

Normalize data by use of ratios based on a changing dataset in R

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!

Answer Source

dplyr is very efficient here:

library(dplyr)
result <- group_by(MasterTable, Time, Type, Treatment) %>%
  mutate(pc = Value / Value[1])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download