[R] how to do this?

Linlin Yan yanlinlin82 at gmail.com
Fri Sep 11 11:19:20 CEST 2009


Try this:
> y[matrix(c(seq_along(x), x), ncol = 2)]
[1]  2 16 12

On Fri, Sep 11, 2009 at 4:17 PM, Luca Braglia <braglia at poleis.eu> wrote:
> Hello R-users
>
> I have a situation like this
>
> x=c(1,3,2)
>
> y=data.frame("a"=1:3, "b"=4:6, "c"=7:9)*2
>
> So we have
>
>> t(t(x))
>     [,1]
> [1,]    1
> [2,]    3
> [3,]    2
>
> And
>
>> y
>  a  b  c
> 1 2  8 14
> 2 4 10 16
> 3 6 12 18
>
> I would like to obtain a vector with number taken from the data.frame: x is needed as row index
>
> With c(1,3,2), in this case the ouput should be
>
> 2
> 16
> 12
>
> I've tried a little bit with apply, but unsuccessfully.
>
> Thank you
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list