1

Supposed that I have the following data:

a1 <- 1
a2 <- 2
a3 <- 3

In my actual data, I have way more variable and what I am aiming to do is create interaction variables between them and then put them into one big dataframe like this:

a1a1 <- a1*a1
a1a2 <- a1*a2
a1a3 <- a1*a3

a2a2 <- a2*a1
a2a3 <- a2*a2

a3a3 <- a3*a3

mydf <- data.frame(a1a1,a1a2,...)

I am certain I could use a forloop to accomplish what I want but I am horribly new to R and coding methodology altogether. My intuition tells me to do something along the lines of this:

 n <- 3
 j <- 3

 for( i in 1:n) {
    k <- 1
    for( j in 1:k) {

 aiaj <- ai*aj 
 #Not sure how to put into a data.frame for each variable
 k <- k + 1 
 }
 }

The way the loop is constructed, I believe it would make the variables a1a1,a1a2,a1a3 on the first iteration, on the second iteration it would make a2a2,a2a3, and then on the third iteration a3a3. I've tried this, and it doesn't work so I am hoping I can get some suggestions. Additionally, I have no idea how I would keep adding to the data.frame

6
  • Do you really need to create those variables? Many R functions can handle these interactions via a formula. Commented Apr 1, 2017 at 0:23
  • Perhaps I do not, I don't really know. But, I do know that if I do create these variables, then I will be able to do what I need to do for the purpose of the original data. Commented Apr 1, 2017 at 0:36
  • For example, If you are trying to generate a linear model with these cross-terms you can use a formula like lm(y ~ (a1+a2+a3)^2, data=data). If the function that you are trying to use accepts formulas. please take a look at the help page ?formula Commented Apr 1, 2017 at 0:41
  • What is the different between a1*a2 and a2*a1? Moreover, why a2*a3 is missing? Commented Apr 1, 2017 at 0:45
  • I corrected the difference. There is no difference between a1*a2, and I added a2*a3. They should all be unique. Commented Apr 1, 2017 at 1:35

2 Answers 2

2

This will store all interactions in a data.frame that can filtered or additional functions used as desired

a <- c(a1, a2, a3)
df1 <- expand.grid(a, a)
df1$prod <- df1[, 1] * df1[, 2]

> df1
  Var1 Var2 prod
1    1    1    1
2    2    1    2
3    3    1    3
4    1    2    2
5    2    2    4
6    3    2    6
7    1    3    3
8    2    3    6
9    3    3    9
Sign up to request clarification or add additional context in comments.

Comments

0

How about you try this:

suppose this is your data frame

q1 <- data.frame(a = sample(1:20, 4), b = sample(1:20, 4),c=sample(1:20, 4))

you can use lapply to interact the columns with this function. There may be efficient methods but I hope this solves your problem.

library(dplyr)

 mn <- lapply(1:3, function(j) data.frame(lapply(1:3, function(i) 
         q1[,i]*q1[,j])))%>%bind_cols()

Column names may not be pretty but you can change them at any time using colnames

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.