Thanks Jeff, It worked!! ________________________________ From: Jeff Newmiller <jdnew...@dcn.davis.ca.us> Sent: Saturday 28 August 2021 16:32 To: r-help@r-project.org <r-help@r-project.org>; Eliza Botto <eliza_bo...@outlook.com>; r-help@r-project.org <r-help@r-project.org> Subject: Re: [R] Finding if numbers fall within a range
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_bo...@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@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. [[alternative HTML version deleted]] ______________________________________________ R-help@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.