R data.frame get value from variable which is selected by another variable, vectorized

前端 未结 4 1598
眼角桃花
眼角桃花 2021-01-25 06:09

I have data that comes to me with many similar variables, with an additional variable which indicates which one of those similar variables I really want. Using a loop I

4条回答
  •  佛祖请我去吃肉
    2021-01-25 06:49

    I noticed this answer from @josliber see (https://stackoverflow.com/a/30279903/4606130) when trying to work on a data.table solution and it seems fast:

    df[cbind(seq(df$var), df$var)]
    
    [1] "3050" "2062" "1036" "4001" "3075" "4083" "1085" "3061"
    

提交回复
热议问题