-----Original Message-----
From: Heymans, MW [mailto:mw.heymans@vumc.nl]
Sent: July 17, 2006 03:52 AM
To: s-news
Subject: replace?
Hi all,
Some help is appreciated with this one.
I have a matrix k with 2 columns: the first 10 rows are:
[,1] [,2]
[1,] 4 0.9952229
[2,] 5 0.9936306
[3,] 6 0.9856688
[4,] 7 0.9824841
[5,] 9 0.9808917
[6,] 10 0.9729299
[7,] 11 0.9713376
[8,] 12 0.9697452
[9,] 13 0.9649682
[10,] 14 0.9538217
I also have a vector x with the following 15 components:
[1] 4 10 7 12 7 10 11 11 12 7 4 7 12 11 10
Now I want to replace the vector components with the values of
the 2nd column of matrix k, related to the values in the 1st column of
this matrix, like this:
[1] 0.9952229 0.9729299 0.9824841 0.9697452 0.9824841 etc.
Thanks in advance,
Martijn
**********************************
Martijn: Like this?,
List members: What is the "vectorized" approach to this type of
solution?
> k <- matrix(c( 4, 0.9952229, 5, 0.9936306, 6, 0.9856688, 7, 0.9824841,
9, 0.9808917,10, 0.9729299,11, 0.9713376,12, 0.9697452,13, 0.9649682,14,
0.9538217), ncol=2, byrow=T)
> x <- xx <- numeric(15)
> x <- c(4, 10, 7, 12, 7, 10, 11, 11, 12, 7, 4, 7, 12, 11, 10)
> for (i in 1:length(x)) xx[i] <- k[k[,1]==x[i], 2]
> k
[,1] [,2]
[1,] 4 0.9952229
[2,] 5 0.9936306
[3,] 6 0.9856688
[4,] 7 0.9824841
[5,] 9 0.9808917
[6,] 10 0.9729299
[7,] 11 0.9713376
[8,] 12 0.9697452
[9,] 13 0.9649682
[10,] 14 0.9538217
> x
[1] 4 10 7 12 7 10 11 11 12 7 4 7 12 11 10
> xx
[1] 0.9952229 0.9729299 0.9824841 0.9697452 0.9824841 0.9729299
0.9713376 0.9713376 0.9697452 0.9824841 0.9952229 0.9824841 0.9697452
[14] 0.9713376 0.9729299
DaveT.
**********************************
Silviculture Data Analyst
Ontario Forest Research Institute
Ontario Ministry of Natural Resources
Sault Ste. Marie, Ontario, Canada
david.thompson@mnr.gov.on.ca
**********************************
|