I'm trying to construct a for loop in R that will evaluate if there's a "TRUE" in a column of a Dataframe and further evaluate from that specific point if "TRUE" appears for another 499 loops. Data
This data is collected from a force platform sampling at 1000 Hz. We had subjects drop off a platform and land on the force platform and maintain their body weight (in Newtons) for 0.5 seconds. My objective is to extract Time To Stability, defined as when someone maintains their body weight within ±5% for 0.5 seconds (body weight fluctuates as they're landing and we want to know when they are 'stable'). Initial setup code:
df <- read.csv("~ANBLD1(TTS).csv") #load data
time <- seq(0.001,length(df$Fz)*.001,0.001) #create time column
f <- mean(tail(df$Fz,50)) #find ~approx body weight
fg <- f*1.05 #find body weight +5%
fl <- f*0.95 #find body weight -5%
g <- df$Fz<fg & df$Fz>fl #Determine where body weight is ±5%
ndf <- cbind(df,time,g) #combine all for ease
m <- min(which(ndf$Fz>0)) #find row when force > 0, they're landing on Force Platform
I think I'm ready for the For Loop at this point, however I'm still a novice at programming. My current and incomplete For Loop:
for(i in 1:length(ndf$g)) {
if(ndf$g[i]=="TRUE"){
x <- nrow(ndf$g[i]) # Save row that "TRUE" was found
to <- ndf$time[i] # Save Initial time that was originally
y <- ndf$time[x]-(ndf$time[x]-0.001) # should be 0.001
for(j in ndf$g[i]:length(ndf$g){
if(y = 0.5){
}
I've thought of setting it up as a Loop where I find in ndf$g a "TRUE", and save that row and time, and loop from that point (I'm thinking a 2nd loop from this point?) and loop it through until 'y' (in my code) is = 0.5, meaning they have maintained their body weight for 0.5 seconds. I would like to extract 2 specific Times, the start row time (I think my code currently does this) and end row time. Anyone able to offer some pointers (not C or C++ pointers) or code to help with this problem?
PS. I can do this by hand in Excel, but it takes time, there are about 20 trials per subject, and about 100 subjects with more data being gathered, so coding this in will save time and cut down on user errors that can be introduced do it manually.
Aucun commentaire:
Enregistrer un commentaire