I have a df "DISC_CONS_Reports" with 6 columns, each with 2 or more factors.
I want to do something very simple, adding a column named 'State' with value 'Conscious_pure' when column 1 contains the string 'yes' AND columns 3 OR 5 OR 6 contain a certain string. I'm new in R, I used the case_when function as follows but it doesn't output what I expected, i.e., it treats the first logical statement (ROR1.Conscious?== "yes" &) as an OR, assigning 'conscious_pure' also to those subjects for which I have in col 1 value 'no' but which contain in at least one of the other cols one of the specified strings (e.g. 'dreaming', 'thinking' etc.):
ROR1_states <- DISC_CONS_Reports %>%
mutate(State=case_when(
`ROR1.Conscious?`== "yes" &
`ROR1.Self-reported_State` == 'dreaming' |
`ROR1.Focus_of_experience` == 'environment' |
`ROR1.Focus_of_experience` == 'myself' |
`ROR1.Visual_or_Thinking` == 'thinking' |
`ROR1.Visual_or_Thinking` == 'seeing' |
`ROR1.Visual_or_Thinking` == 'NA' ~ "Conscious_pure",
`ROR1.Conscious?`=="not arousable (i.e., Disconnected Consciousness ?)" ~ "Not Arousable"))
I would like instead to assign "Conscious_pure" only to those subjects for which ROR1.Conscious?`== "yes" is TRUE AND also at least one of the other conditions (i.e., dreaming,environment, myself, thinking, seeing etc)
You should be able to do this by putting parentheses around the or statements, thus evaluating them as a group first before evaluating the and condition.
One other thing to note, is that if
ROR1.Visual_or_Thinkinghas a string values"NA", then this will work. If the last equality in your or statements is evaluating for a missing valueNA, then you need to replacewith
Since I don't have access to your data, I don't know if this solves all the problems, but it should give you a place to start.