代码之家  ›  专栏  ›  技术社区  ›  Tal Galili

有版本的吗?将“每个”作为向量接收的代表?(R问题)

  •  1
  • Tal Galili  · 技术社区  · 14 年前

    (请忽略这个问题-太愚蠢了…)

    我想做点什么:

    rep(1:3, each = 1:3)
    # And will output this vector:
    c(1,2,2,3,3,3)
    

    它存在吗?(如果是,怎么做?)

    更新:我可以这样写-

    rep2 <- function(x, each)
    {
        output <- NULL
        for(i in 1:length(x))
        {
            output <- c(output, rep(x[i], each = each[i]))
        }
        return(output)
    }
    # example:
    rep2(1:3,1:3)
    

    2 回复  |  直到 14 年前
        1
  •  2
  •   James    14 年前
    rep(1:3,1:3)
    

    那当然是你的榜样。为了获得信息 rep 功能是 times , each 是单个整数(或向量的第一个元素),用于确定 x .

        2
  •  1
  •   Spacedman    14 年前

    嗯这个:

    > rep(1:3, 1:3)
    [1] 1 2 2 3 3 3
    

    您需要的是“times=”参数,而不是“each=”。