1

Suppose I have 4 variables in R that belong to dataset data1 and are named x1, x2, y1, y2, and I want to use a loop to create the following variables zi = xi*yi for i=1,2 that will also belong to data1. These variables are defined as follows:

I was previously coding it as

data1$x1<-c(1,2,3)
data1$x2<-c(2,3,4)
data1$y1<-c(3,4,5)
data1$y2<-c(4,5,6)

I was previously coding the process as

data1$z1<-data1$x1*data1$y1
data1$z2<-data1$x2*data1$y2

This might seem practical for creating only two new variables, but I want to learn how to use a loop for situations where there are many more variables to create with this pattern.

I have tried searching online without success because I don't really know what to search. I know for the creating new variables I can use the paste0 and assign functions, but I am not sure how to reference existing variables using a loop index.

Any help is much appreciated!

0

3 Answers 3

2

Have revised based on revision to question. Assuming data1 is the data frame shown in the Note at the end we get xnames (a character vector of the x column names), ynames and znames. Then multiply data1[xnames] and data1[ynames], set the names of the result to znames and cbind the original data with that.

x_names <- grep("x", names(data1), value = TRUE)
y_names <- grep("y", names(data1), value = TRUE)
z_names <- sub("x", "z", x_names)

cbind(data1, setNames(data1[xnames] * data1[ynames], znames))

giving:

  x1 x2 y1 y2 z1 z2
1  1  2  3  4  3  8
2  2  3  4  5  8 15
3  3  4  5  6 15 24

Note

Input in reproducible form:

data1 <- list()
data1$x1<-c(1,2,3)
data1$x2<-c(2,3,4)
data1$y1<-c(3,4,5)
data1$y2<-c(4,5,6)
data1 <- as.data.frame(data1)
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for my lack of clarity. The way I was previously coding it was as follows: data1$z1<-data1$x1*data1$y1 data1$z2<-data1$x2*data1$y2 and so on. I want to do it with a loop instead.
Generally in R the whole object approach is preferred to loops.
1

Here is what I think you want in a simple for loop. The trick is to use the [[ notation instead of the $ to select your variables.

data1 <- data.frame(x1 = 1:4,
                    x2 = 11:14,
                    x3 = 21:24,
                    y1 = 101:104,
                    y2 = 111:114,
                    y3 = 121:124)

for (index in 1:3) {
  
  data1[[paste0("z", index)]] <- data1[[paste0("x", index)]] * data1[[paste0("y", index)]]
  
}

# > data1
#   x1 x2 x3  y1  y2  y3  z1   z2   z3
# 1  1 11 21 101 111 121 101 1221 2541
# 2  2 12 22 102 112 122 204 1344 2684
# 3  3 13 23 103 113 123 309 1469 2829
# 4  4 14 24 104 114 124 416 1596 2976

Comments

0

Try the code below

x <- mget(ls(pattern = "^x"))
y <- mget(ls(pattern = "^y"))
list2env(setNames(Map("*",x,y),paste0("z",seq_along(x))),envir = .GlobalEnv)

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.