0

I have a 2X2 design (it's more complex, but below a simple example to illustrate my issue).

Say my output data looks like this:

In the image, ID is each participant, block number is the condition they saw, and everybody saw 4 questions.

enter image description here

Where the counterbalancing plays a role (for example) is that the same item (e.g, Item1) was repeated in different positions between Q1-Q4 across participants depending on the block. In the picture above, Item1 would be the red numbers. I used case_when to pull out the combinations and give me a clean column of the Item1 responses:

data <- data %>% mutate(
  Item1= case_when(
    BlockNumber == 'Block1' & Q1 ~ Q1, 
    BlockNumber == 'Block5' & Q2 ~ Q2,
    BlockNumber == 'Block6' & Q3 ~ Q3,
    BlockNumber == 'Block7' & Q4 ~ Q4))

This gives me something like:

enter image description here
The code is doing exactly what I need it to, BUT the ZERO VALUES are getting dropped [highlighted in the picture].

Instead of a zero, it is just an NA value. Something like this: enter image description here

The problem is that zeros are meaningful in my data and I need to keep them.

Does anyone have an idea of what code I could add/use to avoid this? It really messes up my mean scores afterwards because the zeros are dropped.

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
VVO
  • 3
  • 2
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please do not post pictures of data. We can't easily copy/paste that into R for testing. – MrFlick Apr 15 '21 at 21:28
  • 1
    @VVO I think you misunderstand the problem. It's not 0 causing it, but your case when returns null for blocknumber greater than 4. you need to add more cases. – AlienDeg Apr 15 '21 at 21:37
  • I believe it is supposed to say `Block5` to get the number in `Q2` that you have marked with red - is that not correct? – WilliamGram Apr 15 '21 at 21:52
  • I still don't understand the question. In your top table screenshot, `BlockNumber` is a field with numbers in it from 1 to 8. Your tests check if any of those values in BlockNumber match text like 'Block1', which will always be false, since there aren't any values in that column of your table that have the word "Block" in them. I presume either the table of values or the code is not what you're using, otherwise I don't understand how it's working for you. – Jon Spring Apr 15 '21 at 22:11
  • Sorry I just realized the picture doesn't accurately reflect my data. In my data, the BlockNumber column contains factors, "Block1" "Block2" "Block3". The issue as @William Gram picked up was that I was over specifying. All I needed was the BlockNumber== 'Block5' ~ Q1 to tell R where to get the value from. Yet, I was adding the extra bit of info with the &Q1 that was giving me a false, hence returning an NA. – VVO Apr 16 '21 at 18:17
  • Also to the rest of the commenters, apologies! I'll add a reproducible example in the future. – VVO Apr 16 '21 at 18:18

2 Answers2

1
data <- data %>% mutate(
  Item1= case_when(
    BlockNumber == 'Block1' & Q1 ~ Q1, 
    BlockNumber == 'Block4' & Q2 ~ Q2,
    BlockNumber == 'Block6' & Q3 ~ Q3,
    BlockNumber == 'Block7' & Q4 ~ Q4,

    # Get ready for some magic!
    TRUE ~ 0))

If you want to specify a value if the other tests haven't passed, you can use TRUE as your last "test" to use that value.

Otherwise, any case that doesn't pass the prior tests will output NA.

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
0

I believe the reason you are experiencing this is because the integer 0 evaluates to FALSE. That is, in the row where BlockNumber == 'Block5' you have that Q2 == 0, which means you are asking the case BlockNumber == 'Block5' & FALSE which is FALSE. Thus, you get NA for that case.

From your explanation it seems to me you don't actually need the second condition, so what you can do is to simplify your case_when operation:

data %>% mutate(
  Item1= case_when(
    BlockNumber == 'Block1' ~ Q1, 
    BlockNumber == 'Block5' ~ Q2,
    BlockNumber == 'Block6' ~ Q3,
    BlockNumber == 'Block7' ~ Q4))

At least I don't see why you would need the & Qx conditions. But let me know if I misunderstood something.

WilliamGram
  • 673
  • 3
  • 7
  • Ah! Thank you so much!!! That was exactly the issue. I thought I needed to over specify but I didn't need the & Qx, I get my zeroes now. Thank you! – VVO Apr 15 '21 at 21:59