[R] Finding if numbers fall within a range

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Sat Aug 28 18:32:01 CEST 2021


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 using 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 using 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.



More information about the R-help mailing list