Re: [R] Search arrays based on similar values

From: Petr Savicky <savicky_at_praha1.ff.cuni.cz>
Date: Wed, 06 Apr 2011 08:36:29 +0200

On Tue, Apr 05, 2011 at 05:34:50PM -0500, mjdubya wrote:
> Hey folks,
> I have two arrays: "A" (1X100) with non-ordered values ranging 1-14
> "B" (2X14) containing 14 decimal values.
> I would like to create a new array (1X100) that contains only the decimal
> values from array B by associating the integers from A and B. In other
> words, for each value of A find the same integer value of B, select the
> associated decimal value of B.
>
> A B newarray
> 1 1 0.1 0.1
> 1 2 0.3 0.1
> 1 3 0.14 0.1
> 2 4 0.2 0.3
> 3 5 0.82 0.14
> 3 6 0.21 0.14
> 4 . . 0.2
> 7 . . .
> 14 . . .
> 4 14 0.03 .
> 3 .
> 5 .
> . .
> . .

Hi.

The first column of B seems to contain consecutive integers 1:nrow(B). If this is true, then try following

  B <- cbind(1:6, c(0.1, 0.3, 0.14, 0.2, 0.82, 0.21))   A <- rbind(1, 1, 1, 2, 3, 3, 4, 6)
  cbind(B[A[, 1], 2])

Hope this helps.

Petr Savicky.



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. Received on Wed 06 Apr 2011 - 06:43:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 06 Apr 2011 - 20:30:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive