Hi, I'm not very experienced with R and struggle with data selection from a long matrix with two columns. I want to cut out the data between x > 0 and min(y).> x <- c(-10, -5, 0, 5, 10, 15, 20) > y <- c(10, 10, 10, -5, -6, -7, 10) > data <- as.matrix( cbind(x, y) )> datax y [1,] -10 10 [2,] -5 10 [3,] 0 10 _ [4,] 5 -5 |-- data interval [5,] 10 -6 _| to be selected [6,] 15 -7 [7,] 20 10 I need that to select the interval between a time 0 and a peak that is the minimum here to fit that interval via nls() later. Can anybody help? I would be very thankful for general hints how to select data with the help of conditions. Cheers Martin
#is this what you want? x <- c(-10, -5, 0, 5, 10, 15, 20) y <- c(10, 10, 10, -5, -6, -7, 10) data <- data.frame(x, y) subset(data, x>0 & y==min(y)) On Sat, Oct 25, 2008 at 12:33 PM, Martin Ballaschk <qcumba at web.de> wrote:> Hi, > > I'm not very experienced with R and struggle with data selection from a long > matrix with two columns. > > I want to cut out the data between x > 0 and min(y). > >> x <- c(-10, -5, 0, 5, 10, 15, 20) >> y <- c(10, 10, 10, -5, -6, -7, 10) >> data <- as.matrix( cbind(x, y) ) > >> data > x y > [1,] -10 10 > [2,] -5 10 > [3,] 0 10 _ > [4,] 5 -5 |-- data interval > [5,] 10 -6 _| to be selected > [6,] 15 -7 > [7,] 20 10 > > > I need that to select the interval between a time 0 and a peak that is the > minimum here to fit that interval via nls() later. > > Can anybody help? I would be very thankful for general hints how to select > data with the help of conditions. > > Cheers > Martin > > ______________________________________________ > 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. >-- Stephen Sefick Research Scientist Southeastern Natural Sciences Academy Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis
If you only want the first sequence, then this might work:> x <- c(-10, -5, 0, 5, 10, 15, 20) > y <- c(10, 10, 10, -5, -6, -7, 10) > data <- as.matrix( cbind(x, y) ) > # if you only want the first 'run', then use 'rle' to find it > mask <- data[,1] > 0 & data[,2] > min(data[,2]) > run <- rle(mask) > # now either the first or second will be TRUE, so find it > offset <- cumsum(c(1, run$lengths)) # off set of the start of sequence > index <- if (run$values[1]) 1 else 2 > new_data <- data[seq(from=offset[index], length=run$lengths[index]),] > > new_datax y [1,] 5 -5 [2,] 10 -6> datax y [1,] -10 10 [2,] -5 10 [3,] 0 10 [4,] 5 -5 [5,] 10 -6 [6,] 15 -7 [7,] 20 10 On Sat, Oct 25, 2008 at 12:33 PM, Martin Ballaschk <qcumba at web.de> wrote:> Hi, > > I'm not very experienced with R and struggle with data selection from a long > matrix with two columns. > > I want to cut out the data between x > 0 and min(y). > >> x <- c(-10, -5, 0, 5, 10, 15, 20) >> y <- c(10, 10, 10, -5, -6, -7, 10) >> data <- as.matrix( cbind(x, y) ) > >> data > x y > [1,] -10 10 > [2,] -5 10 > [3,] 0 10 _ > [4,] 5 -5 |-- data interval > [5,] 10 -6 _| to be selected > [6,] 15 -7 > [7,] 20 10 > > > I need that to select the interval between a time 0 and a peak that is the > minimum here to fit that interval via nls() later. > > Can anybody help? I would be very thankful for general hints how to select > data with the help of conditions. > > Cheers > Martin > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?