Hi,
I was wondering how to create a new column with values that are dependent on values from another column? For my dataframe, each subject is shown two blocktypes (mouth block or nose block), just in random order. Based on the first blocktype they're shown, we would like to create a new column "blockorder" where all of the data values for each subject are either "mouthfirst" or "nosefirst".
A portion of the dataframe roughly looks like this:
subject | blocktype | trial_index
1 | nose | 6
1 | nose | 9
1 | nose | 12
1 | nose | 15
1 | nose | 18
1 | mouth | 21
1 | mouth | 24
1 | mouth | 27
1 | mouth | 30
2 | mouth | 6
2 | mouth | 9
2 | mouth | 12
2 | mouth | 15
2 | nose | 18
2 | nose | 21
2 | nose | 24
2 | nose | 27
2 | nose | 30
I want the dataframe to look like this:
subject | blocktype | trial_index | block order
1 | nose | 6 | nosefirst
1 | nose | 9 | nosefirst
1 | nose | 12 | nosefirst
1 | nose | 15 | nosefirst
1 | nose | 18 | nosefirst
1 | mouth | 21 | nosefirst
1 | mouth | 24 | nosefirst
1 | mouth | 27 | nosefirst
1 | mouth | 30 | nosefirst
2 | mouth | 6 | mouthfirst
2 | mouth | 9 | mouthfirst
2 | mouth | 12 | mouthfirst
2 | mouth | 15 | mouthfirst
2 | nose | 18 | mouthfirst
2 | nose | 21 | mouthfirst
2 | nose | 24 | mouthfirst
2 | nose | 27 | mouthfirst
2 | nose | 30 | mouthfirst
I tried using the ifelse statement to create a new column with these values. However, the new column would have values that correspond with the blocktype & trial_index and therefore give different values rather than the output the same value for each subject.
RATHER, I want to output that same value for all the rows for each subject (if a subject is shown trial_index = 6 and blocktype = nose, all values for blockorder for that particular subject = nosefirst)
I hope this makes sense, any help is appreciated!