I'm a beginner R user (not entirely sure if it's the most appropriate language for this use) and am having difficulty coming up with a solution for what I think is probably relatively easy.
I have a dataset that looks as follows:
Filename Label seg_Start seg_End t_ms CPP
1 BJB1NRL.mat V 231.537 444.039 230 0.000
2 BJB1NRL.mat V 231.537 444.039 240 0.000
3 BJB1NRL.mat V 231.537 444.039 250 19.553
4 BJB1NRL.mat V 231.537 444.039 260 16.063
5 BJB1NRL.mat V 231.537 444.039 270 18.090
6 BJB1NRL.mat V 231.537 444.039 280 21.547
The fifth column (t_ms) is time in milliseconds, and the third and fourth columns are also time. Once fifth column reaches the value in seg_End (rounded), the third and fourth column change, i.e:
57 BJB1NRL.mat V 554.141 887.246 890 14.125
58 BJB1NRL.mat V 1081.457 1269.050 1080 0.000
59 BJB1NRL.mat V 1081.457 1269.050 1090 0.000
60 BJB1NRL.mat V 1081.457 1269.050 1100 22.398
61 BJB1NRL.mat V 1081.457 1269.050 1110 20.006
62 BJB1NRL.mat V 1081.457 1269.050 1120 17.931
What I have to do is straightfoward to do manually, but I'm struggling to create a script to do it for me. For each segment (i.e. 231-444, then 1081-1269 etc) I need to see how many milliseconds it takes for the value in the final column to reach a threshold value (e.g. 28). That is, I need to count how many rows from the segment starting (t_ms = seg_Start) until the value in the final row reaches this threshold value, since each row is 10ms. If the value never reaches the threshold then a value of 0 should be given.
So far, my code is mostly my attempts to split the dataset up accordingly to be analysed. It is likely inefficient, and more worringly, incorrect.
The dataset has hundreds of different 'filenames' which need to be analysed separately, so I split those up first.
Result <- #The dataset, currently a .csv file
filenames <- unique(Result$Filename)
levels <- nlevels(filenames)
for(i in 1:levels){
nam <- paste("Sub", i, sep = "")
assign(nam, subset(Result, Filename == filenames[i]))
}
Sub <- lapply(ls(pattern="Sub[0-9]+"), function(x) get(x))
# Create list with segment end times
i <- NULL
for(i in 1:levels){
nam <- paste("unique.Sub", i, sep = "")
assign(nam, unique(Sub[[i]]$seg_End))
}
unique.Sub <- lapply(ls(pattern="unique.Sub[0-9]+"), function(x) get(x))
# Round these values to nearest 10
i <- NULL
for(i in 1:levels){
unique.Sub[[i]] <- round(unique.Sub[[i]], digits=-1)
}
# Create list with segment start times
i <- NULL
for(i in 1:levels){
nam <- paste("unique2.Sub", i, sep = "")
assign(nam, unique(Sub[[i]]$seg_Start))
}
unique2.Sub <- lapply(ls(pattern="unique2.Sub[0-9]+"), function(x) get(x))
i <- NULL
for(i in 1:levels){
unique2.Sub[[i]] <- round(unique2.Sub[[i]], digits=-1)
}
# Rename variables for clarity
segStart <- sapply(unique2.Sub, function(x) x);
segEnd <- sapply(unique.Sub, function(x) x)`s
I then think I have to go on to the actual analysis, something like
lapply(Sub, function(i){
if Sub$t_ms < segStart && < segEnd
which.max(Sub$t_ms >= 26)
I hope what I am aiming for makes sense, even if the code I've come up with so far does not. As I said, my impression is that there is an easier way to do the analysis than I think. Any help is hugely appreciated.
Aucun commentaire:
Enregistrer un commentaire