I've seen posts showing how to use apply, sapply, dplyr, plyr, etc. to run a function across every row of a matrix. However, I'm struggling to produce an R script that will allow me to run a function that will treat every row as the input. Here is a some sample data:
> ` Time.course..| timecourse1 | X | X.1 | X.2| X.3 | X.4 | X.5 | X.6 | X.7
1 YORF | 0 | 5.000 |10.000| 15.000 | 20 30.000 |40.000 | 50.000 | 60.000
2 YAL026C |1| 0.7030321 | NA | NA | NA | 0.7130882 | 0.3322182 | 0.2153255 | 0.2264951
It may be hard to tell but essentially I have a time series in my first row 0 to 60 and have corresponding expression levels for some 6000+ genes. I have a function to calculate half life, but I need to loop it thru the whole data frame with the input being each row over the same time values.
Here's what I've been able to do for one row after converting the data frame x to a matrix:
`> y <- x[1,]
> time <- c(0,5,10,15,20,30,40,50,60)
> result <- pk.calc.half.life(conc = y, time = time)
> print(result$half.life)
[1] 17.89096`
But I need a fast efficient way to run this for every row and save that value in a new column on my original data frame. Functions sapply and lapply did not work giving me the error that conc and time have to be the same lengths.
> sapply(x, pk.calc.half.life(x, time = time))
Error in check.conc.time(conc, time) :
Conc and time must be the same length
In addition: Warning message:
In check.conc.time(conc, time) : Negative concentrations found
> lapply(x, pk.calc.half.life(x, time = time))
Error in check.conc.time(conc, time) :
Conc and time must be the same length
In addition: Warning message:
In check.conc.time(conc, time) : Negative concentrations found
apply
will do it for you readily. Anyways, you need to remove the first (non-time) column from your dataset, along with the first row (if I understand correctly that it contains only the time index)
data <- structure(list(Time.course = c("YORF", "YAL026C"),
timecourse1 = c(0, 1),
X = c(5, 0.7030321),
X.1 = c(10, NA),
X.2 = c(15, NA),
X.3 = c(20, NA),
X.4 = c(30, 0.7130882),
X.5 = c(40, 0.3322182),
X.6 = c(50, 0.2153255),
X.7 = c(60, 0.2264951)),
row.names = c(NA, -2L), class = c("data.frame"))
time <- as.numeric(data[1, -1])
half_life <- apply(data[-1,-1], 1, function(x) {
PKNCA::pk.calc.half.life(conc = x, time = time)$half.life
})