[R] Matrix of indexes to extract sparse data in dataframe

David L Carlson dcarlson at tamu.edu
Fri Jun 5 15:57:53 CEST 2015


You can select elements of a matrix using a 2 dimensional matrix that specifies the row/column number of the cells you want to extract:

> c2 <- cbind(seq_len(nrow(c0)), c1)
> c2
       c1
[1,] 1  2
[2,] 2  2
[3,] 3  1
[4,] 4  2
[5,] 5  1
[6,] 6  1
> d1 <- c0[c2]
> d1
[1]  -1  -3  12   2 -23  17

See the help page for [

?'['

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352


-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Sergio Fonda
Sent: Friday, June 5, 2015 8:47 AM
To: John Kane
Cc: R-help
Subject: Re: [R] Matrix of indexes to extract sparse data in dataframe

Thank you, of course but I can't use that form as I told. My question is
about the possibility to enter in a dataframe with a matrix of indices and
get the corresponding values
Thanks again
 Il 05/giu/2015 15:39, "John Kane" <jrkrideau at inbox.com> ha scritto:

> d1  <-  apply(c0, 1, min)  I think does it.
>
> John Kane
> Kingston ON Canada
>
>
> > -----Original Message-----
> > From: sergio.fonda99 at gmail.com
> > Sent: Fri, 5 Jun 2015 15:06:34 +0200
> > To: r-help at r-project.org
> > Subject: [R] Matrix of indexes to extract sparse data in dataframe
> >
> > I would like to avoid a "for loop" to get a vector of data taken from
> > rows of a data frame for specific columns.
> > An example is the following (I can't apply min to every row of df, this
> > is
> > just an example):
> >
> > c0=data.frame(a=c(3,-2,12,7,-23,17) , b=c(-1,-3,14,2,6,19))
> > c1=apply(c0,1,which.min)
> >> c1
> > [1] 2 2 1 2 1 1
> >
> > I would like to get a result like the following call, but without
> > employing a "for loop":
> >
> > d1=c(c0[1,c1[1]], c0[2,c1[2]], c0[3,c1[3]], c0[4,c1[4]], c0[5,c1[5]],
> > c0[6,c1[6]])
> >> d1
> > [1]  -1  -3  12   2 -23  17
> >
> > Thanks a lot for any help!
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
> ____________________________________________________________
> Can't remember your password? Do you need a strong and secure password?
> Use Password manager! It stores your passwords & protects your account.
> Check it out at http://mysecurelogon.com/password-manager
>
>
>

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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