0

I currently have an object "data" that is a list of 10 dataframes (one for each bodypart: nose, head, etc.).

The dataframe for each body part consists of 4 columns: frame, x, y, likelihood.

I'm trying to write a function that extracts only the rows where the value for frame falls <= 900 frames after each of the 20 numbers in an object called toneTime. toneTime is a one-dimensional vector that consists of 20 numbers.

Currently, I'm stuck on the fact that it's not letting me vary the columns/dataframe I select based on the arguments of the function.

ExtractInTone <- function(bodypart){
  data$bodypart$frame <- data$bodypart$frame*0.0333
  
  col_names <- c("tone", "time", "x", "y")
  subset <- data.frame(matrix(nrow = 0, ncol = 4, dimnames = list(NULL, col_names)))
  
  x <- 1
  for(i in 1:nrow(data$bodypart)) {
    if (data$bodypart$frame[i] < toneTime[x + 1] ) {
      if (toneTime[x] <= data$bodypart$frame[i] & data$bodypart$frame[i] <= toneTime[x] + 30) {
        row <- data$bodypart[i]
        tone <- x
        row <- cbind(tone, row)
        subset <- rbind(subset, row)
      }
    } else {
      x <- x + 1
    }
  }
  return(print("Data extracted to subset"))
}

Anyone know if it's possible to have a dynamic variable name in the function so that it selects for whatever name of the bodypart is typed into the first argument of the function?

When I try to manually type in the bodypart name, the code works, but I want to see if there's a way to make it so that I can softcode the name of the bodypart I want.

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. You [can't use `$` with a variable in the right](https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-character-value), use `[[ ]]` instead. – MrFlick Aug 14 '23 at 19:26

0 Answers0