From my dataset original variable size is numeric (can be converted to a numeric where small = 1, medium = 2, large = 3).
id <- c('1','2','3','4', '5')
size <- c('small', 'large', 'small', 'small', 'medium')
dest1 <- c('1', '0', '1', '0', '1')
dest2 <- c('0', '1', '1', '0', '1')
via1 <- c('1', '1', '0', '0', '0')
via2 <- c('1', '0', '1', '0', '1')
value <- c('4', '561', '310', '106', '8')
original <- data.frame(id, size, dest1, dest2, via1, via2, value)
I want to interact in a systematic way, the variable size with variables starting with dest
and via
separately, (in my original dataset I have hundreds of variables starting with these words).
I have tried it manually (SIZExDUMMY) but it takes a lot of time to go like this for all possible interactions.
So finally the new database have to look like interacted. What is your proposal to get this outcome?
size_dest1 <- c('1', '0', '1', '0', '2')
size_dest2 <- c('0', '3', '1', '0', '2')
size_via1 <- c('1', '3', '0', '0', '0')
size_via2 <- c('1', '0', '1', '0', '2')
interacted <- data.frame(id, size, dest1, dest2, via1, via2, value, size_dest1, size_dest2, size_via1, size_via2)
In this way the first interaction is size x dest1 = c(1,3,1,1,2) x c(1,0,1,0,1) = c(1,0,1,0,2) = size_dest1. Same idea applies for size_dest2, ...., size_dest1, size_dest2, ....
Any clue?
Thanks