I am trying to fill a Tidy data.frame according to calculated indices. After creating a vector of indices from calculated values, I can test and confirm that the calculated values are correct. However, when I use that vector to assign values to rows in the data.frame, I can see that the values are not being filled in exactly the right rows (one additional row is being filled that shouldn't be). When I assign the same values to a vector using the from:to
method, the values fill in the correct rows.
When I assign the same values to a vector using the from:to
method, the values fill in the correct rows.
PostPulseIndices <- ((Alpha+StepDuration)/SampleInterval+1) : ((Alpha+StepDuration+0.120)/SampleInterval)
yields the correct results:
> head(PostPulseIndices)
[1] 2801 2802 2803 2804 2805 2806
But when used to fill rows in particular columns in a data.frame, the values are filled in starting one row early:
DataFrame$"A1 min"[PostPulseIndices] <- A1_min
DataFrame$"A2 min"[PostPulseIndices] <- A2_min
> DataFrame[2799:2802,]
A1 min A2 min
2799 NA NA
2800 0.001 8e-04
2801 0.001 8e-04
2802 0.001 8e-04
Why are A1 and A2 filling at row 2800 when they should be filling at row 2801, where the index vector starts, and how can I ensure that the right rows are filled going forward?
EDIT: Per @Gregory 's request for additional code, here are the values used to calculate PostPulseIndices:
Alpha <- 0.02
StepDuration <- 0.120
SampleInterval <- 5e-05
And here is the code used to create the data.frame:
Sweeps <- 1
SweepDuration <- 0.3
x <- seq(SampleInterval, SweepDuration, by = SampleInterval)
ColNames <- c("A1 min", "A2 min")
DataFrame <- setNames(data.frame(matrix(ncol = (length(ColNames)), nrow = (length(x) * Sweeps))), ColNames)