I have a base position sheet that I would like to modify in R based on values in a template. The last step is to subset the base position sheet by the row numbers listed as "hours" in the template (csv).
As an example, the first value in the column "hours" in the template (csv) is "7:22". I would like to read this csv and effectively only keep rows 7:22. I would also like the code to work if the "hour" value was "1, 7:22"
My instinct was to try position[template$hours[1], ] as position[7:22, ] would work, but this returns NAs. With typeof - I found that template$hours[1] is a character whereas 7:22 is an integer, but neither converting template$hours[1] to numeric nor integer works.
Tried:
position[template$hours[1], ]
position[as.numeric(template$hours[1]), ]
position[as.integer(template$hours[1]), ]
position[c(template$hours[1]), ]
in the order tried:
NAs NAs introduced by coercion NAs introduced by coercion NAs
Edit: Thanks for the tip MrFlick
Although this is not exactly how it works, this should effectively be the same.
The "base position sheet" discussed is:
position <- data.frame("Hour" = 1:24, "Position" = 0)
The "template" discussed is:
template <- data.frame(hours = "7:22")
I would like the position sheet to look like:
position <- position[7:22, ]
Or even better:
position$Position[7:22] <- 1
based on the value in:
template$hours[1]