Error applying solution from: adding-a-value-in-a-new-column-for-a-specific-number-of-rows-based-on-the-value-of-single-row

Hello!

I had asked a previous question linked above regarding creating a new column that outputs the same values (for specific rows) based on single rows of data.

I was trying to apply it to a new set of data that was slightly different in its output values, but it appears that I've run into an issue. The new column instead outputs the same list of values for every row, e.g. c(4, 7, 7, 7, 7, 7, 1, 1). The values nested in the list matches the values in the response rows (e.g. the first single row of 'response' = 4, second single row of 'response' = 7).

I encountered this issue with the previous dataframe, and I just arranged by subject & trial_index in order to make sure that the rows were all in order (e.g. first 6 rows = NA, single row = other value, etc.). However, I cannot seem to resolve this issue with my current dataframe.

The new dataframe looks at the following columns, where 'response' would be the single row of data and 'resp' would be the identifier row.

I am thinking it's something to do with the other columns or data types of the other variables in my dataframe? As there are many columns (e.g. subject, trial index, blocktypes, etc.) though I'm not sure if one of the other columns would be a factor in why the function doesn't work this time?

response = c('NA', 'NA', 'NA', 'NA', 'NA', 'NA', '4', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', '7', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', '7', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', '7')
resp = c('d', 'b', 'd', 'b', 'b', 'd', 'NA', 'd', 'b', 'b', 'd', 'd', 'd', 'NA', 'b', 'b', 'd', 'b', 'b', 'b', 'NA', 'd', 'd', 'd', 'b', 'd', 'b', 'NA')

df = data.frame(response, resp)

## match slider responses w/ the rest of the data
add_question_val <- function(d, block_size,
                             value_from ='response', value_to = 'newresponse') {
  # Add new column if it doesn't exist
  if (!'value_to' %in% names(d)) d[[value_to]] <- NA
  # Id question rows
  qs <- which(d$resp == 'NA')
  # get values 
  v <- rep(d[qs, value_from], each = block_size)
  # create ~~matrix~~ vector of indices to update
  m <- unlist(lapply(qs, function(x, y) seq(x-y, x - 1), block_size))
  # Update indices
  d[[value_to]][m] <- v
  d
}

## not present in current df dummy dataframe, but in real dataframe I arranged by subject and trial index so that the rows are all in order (e.g. always 6 NA rows followed by a response row for column 'response')
testdf= df %>%
  arrange(subject, trial_index)

testdf = add_question_val(testdf, block_size = 6, 
                              value_from = 'response', value_to = 'newresponse')

I added a dummy dataframe above just to visualize what I would like the output to be like and the columns/rows that are of interest... The function works perfectly fine on any dummy dataframe I create, but not on my actual data for some reason.

I was wondering if anyone had any insight to solving this issue as I'm a bit lost at what went wrong this time around. Thanks for any insight!

Sorry for posting this newbie question -- I got it sorted out!

The data had to be formatted as a dataframe in order for the function to provide the proper output. :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.