I am very basic user and writing functions or loops is outside of what I feel comfortable.
I am working on a dataset with repeated measurements for several subjects.
I have come across a function that someone wrote which I can use with my database, tested and worked.
The issue I am having is that it only works if I filter out of my dataset the data for each individual before running the function.
I am sure this can be resolved adding some code to extract from the dataset each of the individuals to generate the value calculated by the function, and creating a new dataset... but I have no idea how to do it and after watching several videos I am no closer to a solution than 48h ago...
It's hard to give specific advice without seeing some of your data and the function you want to apply to it. Can out post the data for two subjects and the function? To post data, make the subset then run the dput function and post its output. If your data set with two subjects is called DF. Run
dput(DF)
and post the output.
When posting either function output or the code of the function you want to run, put lines with three back ticks before and after text, like this
```
function output or code goes here
```
# Code for getting time over MIC of a **thereshold** of x (thereshold labelled as th):
f2 <- function(dat,th,logarithmic=FALSE) {
above = 0
w <- dat$conc > th
w[w == FALSE] <- 0
w[w == TRUE] <- 1
w2 <- which(abs(diff(w)) == 1)
if (logarithmic == FALSE) {
for (i in w2) {
n1 <- diff(w)[i] * -1*(dat$time[i+1] + ((dat$time[i]-dat$time[i+1]) *
(th-dat$conc[i+1])/(dat$conc[i]-dat$conc[i+1])))
above <- above + n1
}
}
if (logarithmic == TRUE) {
for (i in w2) {
if (diff(w)[i] == 1) {
n1 <- (dat$time[i+1] + ((dat$time[i]-dat$time[i+1]) *
(th-dat$conc[i+1])/(dat$conc[i]-dat$conc[i+1])))
above <- above - n1
}
if (diff(w)[i] == -1) {
n1 <- (dat$time[i+1] + ((dat$time[i]-dat$time[i+1]) *
(log(th)-log(dat$conc[i+1]))/(log(dat$conc[i])-log(dat$conc[i+1]))))
above <- above + n1
}
}
}
return(above)
}
# Determine what concentration MIC thereshold (th = 4):
f2(dat, th = 4, logarithmic = TRUE)
f2(dat, th = 4, logarithmic = FALSE)
The above gives me the time in hours when the MIC is over 4, but for all subjects combined of the dataset.
What i would like to get is the individual times, and so far I know how to do it only one-by-one:
# Extract one subjet and apply re-run function f2.
dat <- dat %>%
filter(Subject ==1)
f2 <- function(dat,th,logarithmic=FALSE) {
above = 0
w <- dat$conc > th
w[w == FALSE] <- 0
w[w == TRUE] <- 1
w2 <- which(abs(diff(w)) == 1)
if (logarithmic == FALSE) {
for (i in w2) {
n1 <- diff(w)[i] * -1*(dat$time[i+1] + ((dat$time[i]-dat$time[i+1]) *
(th-dat$conc[i+1])/(dat$conc[i]-dat$conc[i+1])))
above <- above + n1
}
}
if (logarithmic == TRUE) {
for (i in w2) {
if (diff(w)[i] == 1) {
n1 <- (dat$time[i+1] + ((dat$time[i]-dat$time[i+1]) *
(th-dat$conc[i+1])/(dat$conc[i]-dat$conc[i+1])))
above <- above - n1
}
if (diff(w)[i] == -1) {
n1 <- (dat$time[i+1] + ((dat$time[i]-dat$time[i+1]) *
(log(th)-log(dat$conc[i+1]))/(log(dat$conc[i])-log(dat$conc[i+1]))))
above <- above + n1
}
}
}
return(above)
}
f2(dat, th = 4, logarithmic = TRUE)
f2(dat, th = 4, logarithmic = FALSE)
The above give me the results I need but i have to run the code for the n individuals making my dataset.
Also the result appears only in the console rather than integrating it to the dataset as a new variable or making a new dataset with the results for each of the individuals.