I have a data table of children where I know one ear was affected but I do not know which one. I want to randomly allocate the issue to either the right ear or the left ear. I do not care which but if the one ear is designated "in" then the other ear must be designated "out".
That is easy I thought for just one child
sample(c("in", "out"), size = 2)
Then I thought that I could simply assign the results to the right and left column.
children <- data.table(child = c("Gertrude", "Penelope", "Joshua", "Sebastian", "Jane", "Ravi"))
children[, c("right", "left") := sample(c("in", "out"), size = 2)]
I have messed around a lot with the code and I keep getting the following message. It is not helping me.
Error in
[.data.table(children, ,:=(c("right", "left"), sample(c("in", : Supplied 2 items to be assigned to 6 items of column 'right'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code.
I also tried adding by=child.
Can you please help me?