【问题标题】:R put for loop into vectorR将for循环放入向量中
【发布时间】:2017-10-31 15:42:46
【问题描述】:

我想将以下 for 循环的输出放入单个向量中。

test=c("A","B","C","D")
for(i in 1:3)
 {e=runif(1,5,10);
  f=round(e);
  g=sample(test,f,TRUE);
  h=paste(g,collapse = "");
    print(h)}

输出:

[1] "BDCCABD"
[1] "DDBAADBBAA"
[1] "DACCAB"

我想得到一个像这样的向量:

i=c("BDCCABD","DDBAADBBAA","DACCAB")

感谢您的帮助

【问题讨论】:

  • 在你的循环中,h[i] = paste(g, collapse = "")

标签: r for-loop vector


【解决方案1】:

只需稍微修改您的代码即可。

set.seed(8632)    # make the results reproducible

i <- sapply(1:3, function(x){
    e = runif(1, 5, 10)
    f = round(e)
    g = sample(test, f, TRUE)
    h = paste(g, collapse = "")
    print(h)
    h
})

i
#[1] "CACDAABCC" "ADDAACA"   "ACCDAACAB"

你真的需要print(h)吗?

编辑。
我刚刚对其进行了测试,以下简化给出了完全相同的结果。

set.seed(8632)    # make the results reproducible

j <- sapply(1:3, function(x){
    f <- sample(5:10, 1)        # this is equivalent to your original code
    g = sample(test, f, TRUE)
    h = paste(g, collapse = "")
    print(h)
    h
})

j
#[1] "CACDAABCC" "ADDAACA"   "ACCDAACAB"

identical(i, j)
#[1] TRUE

【讨论】:

    【解决方案2】:

    你提到向量,那我们就用vector

    V=vector()
    test=c("A","B","C","D")
    for(i in 1:3)
    {e=runif(1,5,10);
    f=round(e);
    g=sample(test,f,TRUE);
    h=paste(g,collapse = "");
    V[i]=h}
    
    V
    [1] "BCCAD"     "CCDCACBAD" "ADCDBCBCC"
    V[1]
    [1] "BCCAD"
    

    【讨论】:

    • 感谢您的快速帮助
    • @WiebkeKn Yw~ :-) 美好的一天
    【解决方案3】:

    我认为是这样的:

    test=c("A","B","C","D")
    h_final<-0
    for(i in 1:3){e=runif(1,5,10);
    f=round(e);
    g=sample(test,f,TRUE);
    h=paste(g,collapse = "");
    h_final[i]<-h
    if(i==3){print(h_final)}
    }
    

    【讨论】:

      【解决方案4】:

      像这样?

      j <- character()
      
      test = c("A", "B", "C", "D")
      for (i in 1:3) {
        e = runif(1, 5, 10)
        f = round(e)
        g = sample(test, f, TRUE)
        h = paste(g, collapse = "")
      
        j <- c(j, h)
      }
      
      print(j)
      
      > print(j)
      [1] "DDDBADBCD" "ABBCBCC"   "BBCAA"
      

      编辑:更简单

      test = c("A", "B", "C", "D")
      for (i in 1:3) {
        e = runif(1, 5, 10)
        f = round(e)
        g = sample(test, f, TRUE)
        h[i] = paste(g, collapse = "")
        }
      
      > print(h)
      [1] "DBDADDD"  "AABDA"    "CDBDDABC"
      

      【讨论】:

        【解决方案5】:

        不是最优雅的方式,特别是如果你有很多迭代但它有效:

        test=c("A","B","C","D")
        k = NA
        for(i in 1:3)
        {e=runif(1,5,10)
        f=round(e)
        g=sample(test,f,TRUE)
        h=paste(g,collapse = "")
        k = append(k,h)
        print(h)}
        
        k <- na.omit(k)
        

        【讨论】:

          【解决方案6】:

          你应该看看vectors in R

          你需要初始化一个空向量,我们称之为test_vector

          test_vector = c()
          test=c("A","B","C","D")
          
          for(i in 1:3)
              {e=runif(1,5,10);
              f=round(e);
              g=sample(test,f,TRUE);
              h=paste(g,collapse = "");
              print(h)
              test_vector <- c(test_vector,h)
              }
          

          请注意,您可以在不使用 for 循环的情况下直接将函数应用于 test 向量。

          【讨论】:

          • 注意,for循环的迭代变量也叫i
          • 你说得对,我只是没看就取了OP的同名。
          猜你喜欢
          • 1970-01-01
          • 2021-12-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多