【问题标题】:Vectorized version of which in R?R中哪个矢量化版本?
【发布时间】:2015-03-22 03:48:36
【问题描述】:

我想运行这个循环:

   householdnums=numeric()
    for(i in 1:length(households)){
        hh = households[i]
        householdnums = c(householdnums,which(hhlist==hh))
    } 

其中户和 hhlist 是一个大向量。问题是家庭在 hhlist 中有重复的元素,我需要保留顺序。例如。

hhlist = c(1,2,3,4,5,6, 8)

households = c(1,2,1,1,4,5,6,8)

householdnums should be:

householdnums
[1] 1 2 1 1 4 6 6 7

【问题讨论】:

  • 我不明白你的家庭人数应该如何1 2 1 1 4 6 6 7
  • 你想要match : 例如match(households, hhlist)
  • 哎呀,我的头! :p OP,我认为我们需要澄清你在这里问/做什么!谢谢
  • which 已矢量化。
  • 匹配正是我想要的!你能发帖吗,我会最好的回答你

标签: r loops matrix vector


【解决方案1】:

匹配:例如匹配(家庭,hhlist)

【讨论】:

    猜你喜欢
    • 2012-06-11
    • 1970-01-01
    • 2022-11-29
    • 2016-05-30
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    相关资源
    最近更新 更多