【问题标题】:Combinations of Iterations Passed through Loop - R通过循环传递的迭代组合 - R
【发布时间】:2018-02-08 04:22:36
【问题描述】:

在创建处理传递的不同输入组合的循环时遇到问题。下面是一个非功能性示例,

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))
}

我知道上面的循环不起作用,但因此我无法理解。这正是我想要使用手头数据的目的。

根据我的理解,另外一个手头的问题 - 1 将通过 i 传递给一个数据帧,然后 2 将通过传递给第二个数据帧。

但是如果我需要组合呢?例如,对于comboTwo,我想为两个输入传递“1, 1”,我想传递“1,2”“2, 1”,“2, 2”来获取由funcTwo生成的所有数据帧组合功能。

【问题讨论】:

    标签: r loops iteration


    【解决方案1】:

    我会在循环外创建一个组合数据框,并在行索引上循环:

    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 
    

    我试图了解您想要做什么,您失败的原因是您无法将数据框分配给向量。您必须将 comboOnecomboTwocombinations 初始化为列表。

    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
    

    另外,您需要将[] 更改为[[]]

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

    结果:

    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
    

    【讨论】:

    • 谢谢!我还编辑了问题以包含 funcOne 和 funcTwo 的函数 - 可以合并生成的数据框吗?
    • 比如for循环里面,whilei = 1,funcOne(input1[i])返回了一个数据框,但是你已经初始化了comboOne &lt;- 0 。所以comboOne[i] &lt;- funcOne(input1[i]) 不起作用,因为无法将数据框分配给向量。我试图了解您想要实现的目标,请参阅我编辑的答案。
    【解决方案2】:

    根据您的要求,这里是带有函数的代码。它有效,并带有警告。

    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))
      }
    

    结果:

      [[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
    

    【讨论】:

    • 谢谢!我还编辑了问题以包含 funcOne 和 funcTwo 的函数 - 可以合并生成的数据框吗?
    猜你喜欢
    • 2019-12-21
    • 2013-01-14
    • 2023-02-23
    • 2020-01-24
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    相关资源
    最近更新 更多