Thanks Ting! Here is what worked for me on R:
# Break the dataframe into a list of dataframes based on 'trial_num'
df_list <- split(data, data$trial_num)
# Add row index as a column to each dataframe
df_list <- lapply(df_list, function(df) {
df$word_position <- 1:nrow(df)
return(df)
})
# Combine the dataframes back together
combined_df <- do.call(rbind, df_list)
# Sort the combined dataframe by 'part_number' and 'trial_num'
combined_df <- combined_df %>% arrange(part_number, trial_num)
# View the resulting dataframe
print(combined_df)
# Break the dataframe into a list of dataframes based on 'trial_num'
df_list <- split(data, data$trial_num)
# Add row index as a column to each dataframe
df_list <- lapply(df_list, function(df) {
df$word_position <- 1:nrow(df)
return(df)
})
# Combine the dataframes back together
combined_df <- do.call(rbind, df_list)
# Sort the combined dataframe by 'part_number' and 'trial_num'
combined_df <- combined_df %>% arrange(part_number, trial_num)
# View the resulting dataframe
print(combined_df)