Home > database >  How to loop over each element of a list with another element of another list in R
How to loop over each element of a list with another element of another list in R

Time:01-12

I have to 2 list such as follow:

List1 <- c("X", "Y","Z")
List2 <- c("Enable", "Status", "Quality")

I am expecting something like this:

X_Enable, X_Status,X_Quality,Y_Enable, Y_Status,Y_Quality, Z_Enable, Z_Status,Z_Quality.

Any recommendation will be helpful for me.Thank you

CodePudding user response:

Here a way to do it:

Data

List1 <- c("X", "Y","Z")
List2 <- c("Enable", "Status", "Quality")

Code

paste(rep(List1,each = length(List2)),List2,sep = "_")

Output

[1] "X_Enable"  "X_Status"  "X_Quality" "Y_Enable"  "Y_Status"  "Y_Quality" "Z_Enable"  "Z_Status"  "Z_Quality"

CodePudding user response:

We may use outer

c(outer(List1, List2, FUN = function(x, y) paste(x, y, sep = "_")))
  • Related