Dear useRs, Is there a way in R to see if the numbers in a matrix-row fall within the given range or not? For example, I have the following data;> dput(EB)structure(c(1, 57, 59, 271, 279, 59, 179, 279, 278, 359, 52, 118, 178, 239, 334), .Dim = c(3L, 5L)) The ranges for which these numbers are to be reserved are; 0-60, 61-120, 121-180, 181-240, 241-300, 301-360 In row 1, the number "1", "57", and "59" fall 2ith the range 0-60. Whereas "271" and "279" falls within the range 241-300. So in total 2 ranges are covered and coding should execute 2 for row one. In row 2, the number "59" falls in the range 0-60, "179" falls in the range 121-180, "279" and "278" fall within the range 241-300, and 359 falls in the range 301-360. In total 4 ranges are covered for row 2. In the like-wise pattern, 5 ranges are covered in row 3. So, what I want is a matrix that looks in the following> dput(EBI)structure(c(2, 4, 5), .Dim = c(3L, 1L)) Is there an easy way of doing it? I thank you all very much in advance EB [[alternative HTML version deleted]]
Hello, I think I've found a solution, but it's not producing the same answer as what you're expecting. I think you might've mixed up row and column a few times, but you should be able to alter the following to your needs. Also, see ?.bincode: EB <- matrix(data = c( 1, 271, 179, 359, 178, 57, 279, 279, 52, 239, 59, 59, 278, 118, 334 ), nrow = 3, ncol = 5, byrow = TRUE) # ranges <- list( # c( 0, 60), # c( 61, 120), # c(121, 180), # c(181, 240), # c(241, 300), # c(301, 360) # ) breaks <- seq.int(0, 360, 60) codes <- .bincode(EB, breaks, include.lowest = TRUE) dim(codes) <- dim(EB) num.ranges <- apply(codes, 1, function(xx) length(unique(xx))) I hope this helps! On Sat, Aug 28, 2021 at 11:57 AM Eliza Botto <eliza_botto at outlook.com> wrote:> Dear useRs, > > Is there a way in R to see if the numbers in a matrix-row fall within the > given range or not? For example, I have the following data; > > > dput(EB) > > structure(c(1, 57, 59, 271, 279, 59, 179, 279, 278, 359, 52, > 118, 178, 239, 334), .Dim = c(3L, 5L)) > > The ranges for which these numbers are to be reserved are; > > 0-60, 61-120, 121-180, 181-240, 241-300, 301-360 > > In row 1, the number "1", "57", and "59" fall 2ith the range 0-60. Whereas > "271" and "279" falls within the range 241-300. So in total 2 ranges are > covered and coding should execute 2 for row one. > > In row 2, the number "59" falls in the range 0-60, "179" falls in the > range 121-180, "279" and "278" fall within the range 241-300, and 359 falls > in the range 301-360. In total 4 ranges are covered for row 2. > > In the like-wise pattern, 5 ranges are covered in row 3. > > So, what I want is a matrix that looks in the following > > > dput(EBI) > > structure(c(2, 4, 5), .Dim = c(3L, 1L)) > > Is there an easy way of doing it? > > I thank you all very much in advance > > EB > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
You messed up the dput somehow... but I think this works: m <- t(structure(c(1, 57, 59, 271, 279, 59, 179, 279, 278, 359, 52, 118, 178, 239, 334), .Dim = c(5L, 3L))) brk <- c( 0, 60, 120, 180, 240, 300 ) mc <- matrix( findInterval( m, brk ), ncol = ncol(m) ) m mc DBI <- apply( mc, 1, function(v) length( unique( v ) ) ) On August 28, 2021 8:57:09 AM PDT, Eliza Botto <eliza_botto at outlook.com> wrote:>Dear useRs, > >Is there a way in R to see if the numbers in a matrix-row fall within the given range or not? For example, I have the following data; > >> dput(EB) > >structure(c(1, 57, 59, 271, 279, 59, 179, 279, 278, 359, 52, >118, 178, 239, 334), .Dim = c(3L, 5L)) > >The ranges for which these numbers are to be reserved are; > >0-60, 61-120, 121-180, 181-240, 241-300, 301-360 > >In row 1, the number "1", "57", and "59" fall 2ith the range 0-60. Whereas "271" and "279" falls within the range 241-300. So in total 2 ranges are covered and coding should execute 2 for row one. > >In row 2, the number "59" falls in the range 0-60, "179" falls in the range 121-180, "279" and "278" fall within the range 241-300, and 359 falls in the range 301-360. In total 4 ranges are covered for row 2. > >In the like-wise pattern, 5 ranges are covered in row 3. > >So, what I want is a matrix that looks in the following > >> dput(EBI) > >structure(c(2, 4, 5), .Dim = c(3L, 1L)) > >Is there an easy way of doing it? > >I thank you all very much in advance > >EB > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.-- Sent from my phone. Please excuse my brevity.