Hello, I have two vectors of length = 10 x<-c(2,14,79,27,3,126,15,1,12,4) y<-rep(4,10) and I would like to create a third vector of length = 10 that contains the smallest value at each position in the two above vectors. I have tried: z<-min(x,y) but that doesn't work. With the example data above, the third vector would look like this.> z[1] 2 4 4 4 3 4 4 1 4 4 Any help with this would be much appreciated, thanks! mark [[alternative HTML version deleted]]
Hey Mark, I am sure there are other ways but one way would be: data <- cbind(x,y) #put the two vectors in a matrix> apply(data, 1, min) # use apply() to apply min() to each row[1] 2 4 4 4 3 4 4 1 4 4 Best regards, Josh On Sat, Apr 17, 2010 at 4:14 PM, Mark Na <mtb954@gmail.com> wrote:> Hello, > > I have two vectors of length = 10 > > x<-c(2,14,79,27,3,126,15,1,12,4) > y<-rep(4,10) > > and I would like to create a third vector of length = 10 that contains the > smallest value at each position in the two above vectors. > > I have tried: > > z<-min(x,y) > > but that doesn't work. > > With the example data above, the third vector would look like this. > > > z > [1] 2 4 4 4 3 4 4 1 4 4 > > Any help with this would be much appreciated, thanks! > > mark > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/ [[alternative HTML version deleted]]
On 17-Apr-10 23:14:32, Mark Na wrote:> Hello, > I have two vectors of length = 10 > > x<-c(2,14,79,27,3,126,15,1,12,4) > y<-rep(4,10) > > and I would like to create a third vector of length = 10 that > contains the smallest value at each position in the two above vectors. > > I have tried: > > z<-min(x,y) > > but that doesn't work. > > With the example data above, the third vector would look like this. > >> z > [1] 2 4 4 4 3 4 4 1 4 4 > > Any help with this would be much appreciated, thanks! > > markWhat you need is pmin()>From ?min :'max' and 'min' return the maximum or minimum of _all_ the values present in their arguments Note "all": in other words, your min(x,y) is equivalent to min(c(2,14,79,27,3,126,15,1,12,4,4,4,4,4,4,4,4,4,4,4)) (which = 1). Further from ?min : 'pmax' and 'pmin take one or more vectors (or matrices) as arguments and return a single vector giving the 'parallel' maxima (or minima) of the vectors. The first element of the result is the maximum (minimum) of the first elements of all the arguments, the second element of the result is the maximum (minimum) of the second elements of all the arguments and so on. Hence pmin() ("p" for "parallel"). Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 18-Apr-10 Time: 00:28:16 ------------------------------ XFMail ------------------------------