Walcerz, Douglas (APG)
2009-May-07 13:55 UTC
[R] Plotting counts vs. intervals for a shingle
Hello! Suppose I have a set of values: a <- c(1:10, 5:10) Suppose I also have a set of intervals: b <- cbind(c(0,2.5,4.5,6.5), c(5.5,7.5,9.5,11)) I can create a shingle that counts how many values are in each interval: c <- shingle(a, b) I can display the shingle to see the counts: summary(c, showValues=FALSE) The display looks like this: Intervals: min max count 1 0.0 5.5 6 2 2.5 7.5 8 3 4.5 9.5 10 4 6.5 11.0 8 Overlap between adjacent intervals: [1] 4 6 6 I would like to plot the "count" vs. the "Intervals" I can create a vector representing the intervals: labels <- as.character(levels(c)) But I can't seem to create a vector of counts, which would permit me to plot counts vs. intervals. Thanks for any insights you can provide. p.s. My real data contains 25,094 values and 5,809 intervals. Many of the intervals will not contain any of the values. -Douglas
Took a bit of inspecting, looking at hidden functions, but this seems to do it: library(lattice) a <- c(1:10, 5:10) b <- cbind(c(0,2.5,4.5,6.5), c(5.5,7.5,9.5,11)) c <- shingle(a, b) summary(c, showValues=FALSE) apply(as.matrix(levels(c)), 1, function(x) length(c[ c>= x[[1]][1] & c <= x[[1]][2] ]) ) #[1] 6 8 10 8 "apply" passes a list to the function which requires the "[[" operation before the index. Since you did not create an example that represents the exceptions, i did not test for any such conditions. (Why do people not construct proper examples?) -- David On May 7, 2009, at 9:55 AM, Walcerz, Douglas (APG) wrote:> Hello! > > Suppose I have a set of values: > > a <- c(1:10, 5:10) > > Suppose I also have a set of intervals: > > b <- cbind(c(0,2.5,4.5,6.5), c(5.5,7.5,9.5,11)) > > I can create a shingle that counts how many values are in each > interval: > > c <- shingle(a, b) > > I can display the shingle to see the counts: > > summary(c, showValues=FALSE) > > The display looks like this: > > Intervals: > min max count > 1 0.0 5.5 6 > 2 2.5 7.5 8 > 3 4.5 9.5 10 > 4 6.5 11.0 8 > > Overlap between adjacent intervals: > [1] 4 6 6 > > I would like to plot the "count" vs. the "Intervals" > > I can create a vector representing the intervals: > > labels <- as.character(levels(c)) > > But I can't seem to create a vector of counts, which would permit me > to plot counts vs. intervals. > > Thanks for any insights you can provide. > > p.s. My real data contains 25,094 values and 5,809 intervals. Many > of the intervals will not contain any of the values. > > -DouglasDavid Winsemius, MD Heritage Laboratories West Hartford, CT
On Fri, May 8, 2009 at 1:30 PM, David Winsemius <dwinsemius at comcast.net> wrote:> > Took a bit of inspecting, looking at hidden functions, but this seems to do > it: > > library(lattice) > ?a <- c(1:10, 5:10) > ?b <- cbind(c(0,2.5,4.5,6.5), c(5.5,7.5,9.5,11)) > ?c <- shingle(a, b) > ?summary(c, showValues=FALSE) > > apply(as.matrix(levels(c)), 1, function(x) length(c[ c>= x[[1]][1] & c <> x[[1]][2] ]) ) > > #[1] ?6 ?8 10 ?8 > > "apply" passes a list to the function which requires the "[[" operation > before the index. Since you did not create an example that represents the > exceptions, i did not test for any such conditions. (Why do people not > construct proper examples?)Noting that> str(levels(c))List of 4 $ : num [1:2] 0 5.5 $ : num [1:2] 2.5 7.5 $ : num [1:2] 4.5 9.5 $ : num [1:2] 6.5 11 - attr(*, "class")= chr "shingleLevel" I would suggest the slightly simpler> sapply(levels(c), function(x) sum(x[1] <= c & c <= x[2]))[1] 6 8 10 8 -Deepayan
On May 8, 2009, at 4:55 PM, Deepayan Sarkar wrote:> On Fri, May 8, 2009 at 1:30 PM, David Winsemius <dwinsemius at comcast.net > > wrote: >> >> Took a bit of inspecting, looking at hidden functions, but this >> seems to do >> it: >> >> library(lattice) >> a <- c(1:10, 5:10) >> b <- cbind(c(0,2.5,4.5,6.5), c(5.5,7.5,9.5,11)) >> c <- shingle(a, b) >> summary(c, showValues=FALSE) >> >> apply(as.matrix(levels(c)), 1, function(x) length(c[ c>= x[[1]][1] >> & c <>> x[[1]][2] ]) ) >> >> #[1] 6 8 10 8 >> >> "apply" passes a list to the function which requires the "[[" >> operation >> before the index. Since you did not create an example that >> represents the >> exceptions, i did not test for any such conditions. (Why do people >> not >> construct proper examples?) > > Noting that > >> str(levels(c)) > List of 4 > $ : num [1:2] 0 5.5 > $ : num [1:2] 2.5 7.5 > $ : num [1:2] 4.5 9.5 > $ : num [1:2] 6.5 11 > - attr(*, "class")= chr "shingleLevel" > > I would suggest the slightly simpler > >> sapply(levels(c), function(x) sum(x[1] <= c & c <= x[2])) > [1] 6 8 10 8Much more readable. In looking at my trail of failed efforts, I see that I came close but failed to realize that I was almost at: sapply(as.matrix(levels(c)), function(x) length(c[ c>= x[1] & c <= x[2] ]) ) ... which also succeeds (as does your solution) in producing zeros when no elements are in the intervals.> > > -DeepayanDavid Winsemius, MD Heritage Laboratories West Hartford, CT