1
votes

I have a dataframe as follows:

dput(head(modellingdata, n = 5))

structure(list(X = 1:5, heading = c(2, 0.5, 2, 1.5, 2), StartFrame = c(27L, 
28L, 24L, 31L, 35L), StartYaw = c(0.0719580421911421, 0.0595571128205128, 
0.0645337707459207, 0.0717132524475524, 0.066818187062937), FirstSteeringTime = c(0.433389999999999, 
0.449999999999989, 0.383199999999988, 0.499899999999997, 0.566800000000001
), pNum = c(1L, 1L, 1L, 1L, 1L), EarlyResponses = c(FALSE, FALSE, 
FALSE, FALSE, FALSE), PeakFrame = c(33L, 34L, 32L, 38L, 46L), 
    PeakYaw = c(0.201025641025641, 0.140734297249417, 0.187890472913753, 
    0.154032698135198, 0.23129368951049), PeakSteeringTime = c(0.533459999999998, 
    0.550099999999986, 0.516700000000014, 0.616600000000005, 
    0.750100000000003), heading_radians = c(0.0349065850398866, 
    0.00872664625997165, 0.0349065850398866, 0.0261799387799149, 
    0.0349065850398866), error_rate = c(2.86537083478438, 11.459301348013, 
    2.86537083478438, 3.82015500141104, 2.86537083478438), error_growth = c(0.34899496702501, 
    0.0872653549837393, 0.34899496702501, 0.261769483078731, 
    0.34899496702501)), row.names = c(NA, 5L), class = "data.frame")

Each row of my df is a trial. Overall, I have 3037 rows (trials). pNum denotes the participant number - I have 19 participants overall.

I also have a dataframe of intercepts for each participant:

dput(head(heading_intercept, n = 19))
c(0.432448612242496, 0.446371667203615, 0.420854119185846, 0.366763485495426, 
0.355619586392715, 0.381658477093055, 0.512552445721875, 0.317210665852951, 
0.358345666677048, 0.421441965798511, 0.477135103908373, 0.325512003640487, 
0.5542144068862, 0.454182438162137, 0.333993738757344, 0.424179318544432, 
0.272486598058728, 0.37014581658542, 0.397112817663261)

What I want do is create a new column "intercept" in my modellingdata dataframe. If pNum is 1, I want to select the first intercept in the heading_intercept dataframe and input that value for every row where pNum is 1. When pNum is 2, I want to input the second intercept value into every row where pNum is 2. And so on...

I have tried this:

for (i in c(1:19)){
  if (modellingdata$pNum == i){
    modellingdata$intercept <- c(heading_intercept[i])
  }
}

However this just inputs the first heading_intercept value for every row and every pNum. Does anybody have any ideas? Any help is appreciated!

1

1 Answers

1
votes
modellingdata$intercept <- heading_intercept[modellingdata$pNum]

Or with minimum modification of your current loop:

modellingdata$intercept <- 0L
for (i in c(1:19)){
  rows <- modellingdata$pNum == i
  if (any(rows)) {
    modellingdata$intercept[rows] <- heading_intercept[i]
  }
}