0

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]
  • 1
    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. – MrFlick Oct 28 '19 at 14:56
  • I'm still not sure exactly what you mean. If hour just has values 1,2,3,...,24, where would 7:22 go? Are you adding in new rows? Do you have hour/minute resolution in the hours column? Are you working with strings or numbers? – MrFlick Oct 28 '19 at 15:09
  • Apologies, I think I complicated the matter by having an "Hour" column in the position data frame as well as the template data frame. The issue is that the template data frame is really read from a csv, so the values in "hours" there are read as strings. With this value a string, I cannot subset like position[7:22, ] via position[template$hours[1], ] even if template$hours[1] = "7:22". – user3342589 Oct 28 '19 at 15:15
  • So, as near as I can tell, you want to get R to interpret the string `"7:22"` as the sequence `7, 8, 9, ..., 22` (i.e., the result of the expresssion `7:22`). You could try `eval(parse(text = "7:22"))`, or `eval(parse(text = template$hours[1]))`, perhaps-- but be careful with this---if you really much on `eval(parse())` you will dig yourself into some very confusing holes. – Gregor Thomas Oct 28 '19 at 15:21
  • That seems to be working for now! Thanks for the help To solve for the case when template$hours[i] = "1, 7:22", the final code looks like this: for (j in 1:length(c(unlist(strsplit(template$hours[i], ","))))){position$Position[eval(parse(text = unlist(strsplit(template$hours[i], ","))[j]))] <- 1} – user3342589 Oct 28 '19 at 15:43

0 Answers0