S31
S31

Reputation: 934

Combinations of Iterations Passed through Loop - R

Having trouble creating a loop that deals with different combinations of inputs being passed through. Below is an nonfunctional example,

funcOne <- function(input){
  test <- data.frame(c = input, d = c(1:2))
  return(test)
}

funcTwo <- function(input2, input2.2){
  testing <- data.frame(a = input2, b = c(6:7))

  testing$why <- ifelse(input2.2 == 0, testing$why <- c(1:2), testing$why <- c(3:4))
  return(testing)
}

input1 <- c("ten", "four")
input2 <- c("sara", "john")
input2.2 <- c(0:1)
comboOne <- 0 
comboTwo <- 0
combinations <- 0

for(i in 1:2){
  comboOne[i] <- funcOne(input1[i])
  comboTwo[i] <- funcTwo(input2[i], input2.2[i])

  print(combinations[[i]] <- list(comboOne, comboTwo))
}

I'm aware the loop above doesn't work, but hence why I'm having trouble understanding. That's just what I'm going for with the data at hand.

Additionally another issue at hand, from my understanding - 1 would be passed through i to give one dataframe, and then 2 would be passed through to give the second dataframe.

But what if I needed combinations? For instance, for comboTwo, I wanted to pass "1, 1" for both inputs and I wanted to pass "1 ,2" "2, 1", "2, 2" to get all the combinations of dataframes generated by the funcTwo function.

Upvotes: 0

Views: 80

Answers (2)

Michael Vine
Michael Vine

Reputation: 335

As Per Your request, here is the code with the functions. It works, with warnings.

funcOne <- function(input){
    test <- data.frame(c = input, d = c(1:2))
    return(test)
  }

  funcTwo <- function(input2, input2.2){
    testing <- data.frame(a = input2, b = c(6:7))

    testing$why <- ifelse(input2.2 == 0, testing$why <- c(1:2), testing$why <- c(3:4))
    return(testing)
  }

  input1 <- c("ten", "four")
  input2 <- c("sara", "john")
  input2.2 <- c(0:1)
  comboOne <- 0 
  comboTwo <- 0
  combinations <- 0

  for(i in 1:2){
    comboOne <- funcOne(input1[i])
    comboTwo <- funcTwo(input2[i], input2.2[i])

    print(combinations<- list(comboOne, comboTwo))
  }

results in:

  [[1]]
  c d
  1 ten 1
  2 ten 2

  [[2]]
  a b why
  1 sara 6   1
  2 sara 7   1

  [[1]]
  c d
  1 four 1
  2 four 2

  [[2]]
  a b why
  1 john 6   3
  2 john 7   3

Upvotes: 0

Yifu Yan
Yifu Yan

Reputation: 6116

I would create a data frame of combinations outside the loop, and loop on row index:

combs <- expand.grid(input1,input2,stringsAsFactors = FALSE)
combs 

Var1 Var2
1  ten sara
2 four sara
3  ten john
4 four john

for (i in 1 :nrow(combs)){
    cat(combs[[i,1]]," ",combs[[i,2]],"\n")
}

ten   sara 
four   sara 
ten   john 
four   john 

I tried to understand what you want to do, the reason you failed is that you cannot assign a dataframe to a vector. you must initialize comboOne,comboTwo and combinations as lists.

comboOne <- list() 
comboTwo <- list()
combinations <- list()

combs <- expand.grid(input1,input2,input2.2,stringsAsFactors = FALSE)
combs 
Var1 Var2 Var3
1  ten sara    0
2 four sara    0
3  ten john    0
4 four john    0
5  ten sara    1
6 four sara    1
7  ten john    1
8 four john    1

Also, you need to change [] to [[]]:

for(i in 1 :nrow(combs)){
    dataframe1 <- funcOne(combs[i,"Var1"])
    dataframe2 <- funcTwo(combs[i,"Var2"],combs[i,"Var3"])
    combinations[[i]] <- list(dataframe1,dataframe2)
}

Result:

combinations[1]
[[1]]
[[1]][[1]]
c d
1 ten 1
2 ten 2

[[1]][[2]]
a b why
1 sara 6   1
2 sara 7   1

Upvotes: 1

Related Questions