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

你说的曾经没有我的故事 提交于 2019-12-08 14:00:19

问题


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!


回答1:


dplyr is very efficient here:

library(dplyr)
result <- group_by(MasterTable, Time, Type, Treatment) %>%
  mutate(pc = Value / Value[1])


来源:https://stackoverflow.com/questions/45317530/normalize-data-by-use-of-ratios-based-on-a-changing-dataset-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!