Dennis Gray
Dennis Gray

Reputation: 11

Using R + For Loops to Create Variables

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

Upvotes: 1

Views: 95

Answers (2)

manotheshark
manotheshark

Reputation: 4367

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

Upvotes: 2

Kay
Kay

Reputation: 2332

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

Upvotes: 0

Related Questions