0

I need to insert new numeric variables that are Interactions of the data frame's variables.I managed to it manually by calling the variables by names but how can I do it without calling them by thier names?

 dat <- read.table(text = " IndexRow TargetVar  chairs    tables     lamps  vases
    1     0        0        0         7  9
    2     0        0        1         1  6 
    3     0        1        0         3  5 
    4     0        1        1         7  8 
    5     1        0        0         5  4
    6     1        0        1         1  3 
    7     1        1        0         0  7
    8     1        1        1         6  6
    9     0        0        0         8  9  ", header = TRUE) 

If I enter the variables manually the following line of code will work as expected: dat<-cbind(dat,data.frame(model.matrix(~(chairs+ tables)^2-1,dat))) But I can't figuring out how to insert the variables without calling them by thier names. I tried the following two lines of code with out success, Any Ideas how to solve this issue?
try1:

dat<-model.matrix(~(dat[,1:ncol(dat)])^2-1,data =dat )

try2:

e<-for (i in names(dat)) function(x) {model.matrix(~(dat[,i])^2-1,dat[,i])}
1
  • I think you could construct the formula like this: x <- c("chairs", "tables"); f <- as.formula(paste0("~0+", paste0(x, collapse="*"))); model.matrix(f, dat).
    – lukeA
    Nov 22, 2015 at 17:16

1 Answer 1

2

Subset the data.frame and then create main factors and first-order interactions from all variables:

as.data.frame(model.matrix(~ (. + .)^2 - 1, dat[, 3:5]))
#  chairs tables lamps chairs:tables chairs:lamps tables:lamps
#1      0      0     7             0            0            0
#2      0      1     1             0            0            1
#3      1      0     3             0            3            0
#4      1      1     7             1            7            7
#5      0      0     5             0            0            0
#6      0      1     1             0            0            1
#7      1      0     0             0            0            0
#8      1      1     6             1            6            6
#9      0      0     8             0            0            0
5
  • Thanks @Roland, one q If I may: Is there a way to replace dat[, 3:5] with something that will not require to state the columns number, my full code has dinamic number of columns. Nov 22, 2015 at 17:39
  • I do not understand your comment.
    – Roland
    Nov 22, 2015 at 17:41
  • 1
    I think OP just wants to be shown how to use the column names instead of the column numbers, since the numbers may differ between data sets. dat[c("chairs", "tables", "lamps")] Nov 22, 2015 at 17:44
  • 1
    @RichardScriven But they show in their question how to use column names? Or maybe they want to pass column names programmatically? Then they should read help("[").
    – Roland
    Nov 22, 2015 at 17:47
  • I change your code a bit to have it as I need: as.data.frame(model.matrix(~ (. + .)^2 - 1, dat[, 3:ncol(dat)])) . Thank you.. Nov 22, 2015 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.