Dear R people: I have a vector which runs from -1 to 1, by .1, inclusively. Easy to set up. x <- seq(-1,1,.1) I then sample 3 numbers from x. y <- sample(x, 3) Suppose one of my values is -0.7. I want to set up an interval around that y1 <- pmax(y-0.1,-1) y2 <- pmin(y+0.1,1) For the value -.7, the interval will run from -.8 to -.6. There will be several intervals. Again, nothing interesting so far. However, now I want to set up something that I will call r1. Essentially, r1 "runs" between -1 and 1 as well. I want to place a value in r1 between -.8 and -.6 for my interval, and zeros elsewhere. I'm trying to use a data frame in which the first column represents the values from -1 to 1. The remaining columns are initially set to zero. Also, I'm trying to avoid loops. Any suggestions would be much appreciated. Thanks in advance, Sincerely Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: hodgess at gator.uhd.edu
What do you intend r1 to contain? TRUE and FALSE (an indicator vector?) or say integers 1,2,3 designating your intervals around your 3 sample points? So you would e.g. set r1[k,1] to 3 if the kth grid point in [-1,1] lies in interval 3? Etc? Some more detail would help narrow down the possibilities. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Erin Hodgess Sent: Monday, June 06, 2005 11:51 AM To: r-help at stat.math.ethz.ch Subject: [R] intervals and data frames Dear R people: I have a vector which runs from -1 to 1, by .1, inclusively. Easy to set up. x <- seq(-1,1,.1) I then sample 3 numbers from x. y <- sample(x, 3) Suppose one of my values is -0.7. I want to set up an interval around that y1 <- pmax(y-0.1,-1) y2 <- pmin(y+0.1,1) For the value -.7, the interval will run from -.8 to -.6. There will be several intervals. Again, nothing interesting so far. However, now I want to set up something that I will call r1. Essentially, r1 "runs" between -1 and 1 as well. I want to place a value in r1 between -.8 and -.6 for my interval, and zeros elsewhere. I'm trying to use a data frame in which the first column represents the values from -1 to 1. The remaining columns are initially set to zero. Also, I'm trying to avoid loops. Any suggestions would be much appreciated. Thanks in advance, Sincerely Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: hodgess at gator.uhd.edu ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Hi, I thought this would be close to what you wanted: x <- seq(-1,1,.1) y <- sample(x, 3) r1 <- vector(mode = "numeric", length = length(x)) sapply(y, function(k) { y1 <- max(k - 0.1, -1) y2 <- min(k + 0.1, 1) r1[x >= y1 & x <= y2] <- 1 r1 }) but found that it's not replacing 3 elements of r1 consistently. There seems to be a problem with indexing in that last assignment. Can somebody please explain why indexing by such a logical vector doesn't work here? Regards, Sebastian Erin Hodgess <hodgess at gator.dt.uh.edu> wrote:> Dear R people: > > I have a vector which runs from -1 to 1, by .1, inclusively. > > Easy to set up. x <- seq(-1,1,.1) > > I then sample 3 numbers from x. > y <- sample(x, 3) > > Suppose one of my values is -0.7. I want to set up an interval > around that > y1 <- pmax(y-0.1,-1) > y2 <- pmin(y+0.1,1) > For the value -.7, the interval will run from -.8 to -.6. > There will be several intervals. > > Again, nothing interesting so far. > > However, now I want to set up something that I will call > r1. Essentially, r1 "runs" between -1 and 1 as well. > I want to place a value in r1 between -.8 and -.6 for my interval, > and zeros elsewhere. > > I'm trying to use a data frame in which the first column represents > the values from -1 to 1. The remaining columns are initially set to > zero. Also, I'm trying to avoid loops. > > Any suggestions would be much appreciated. > > Thanks in advance, > Sincerely > Erin Hodgess > Associate Professor > Department of Computer and Mathematical Sciences > University of Houston - Downtown > mailto: hodgess at gator.uhd.edu > > ______________________________________________ R-help at stat.math.ethz.ch > mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read > the posting guide! http://www.R-project.org/posting-guide.html-- Sebastian P. Luque