Bingo!!!
This time it worked....
Thanks arun....
Thankyou very much indeed..
:D
Elisa
> Date: Fri, 24 May 2013 17:03:33 -0700
> From: smartpink111@yahoo.com
> Subject: Re: [R] Continuous columns of matrix
> To: eliza_botto@hotmail.com
> CC: wdunlap@tibco.com; r-help@r-project.org
>
> Hi,
>
> I changed Bill's code little bit (without the plot). I hope it works
in other cases.
>
> lst1<-lapply(split(mat,col(mat)),function(x){big<- x>0.8*max(x);
n<- length(big);startRunOfBigs<- which(c(big[1],!big[-n] & big[-1]));
endRunOfBigs<- which(c(big[-n] & !big[-1], big[n]));index<-
vapply(seq_along(startRunOfBigs),function(i)
which.max(x[startRunOfBigs[i]:endRunOfBigs[i]])+startRunOfBigs[i]-1L,0L);
index<-ifelse(sum(is.na(match(index,c(1,12))))==0 &
x[index]!=max(x[index]),
NA,index);data.frame(Index=index[!is.na(index)],Value=x[index[!is.na(index)]])
})
> lst1
> $`1`
> Index Value
> 1 5 1.939064
> 2 12 1.791503
>
> $`2`
> Index Value
> 1 5 1.508740
> 2 11 1.670409
>
> $`3`
> Index Value
> 1 5 1.463772
> 2 11 1.494151
>
> $`4`
> Index Value
> 1 12 2.245474
>
> $`5`
> Index Value
> 1 6 1.592817
> A.K.
>
>
>
> ________________________________
> From: eliza botto <eliza_botto@hotmail.com>
> To: "smartpink111@yahoo.com" <smartpink111@yahoo.com>
> Sent: Friday, May 24, 2013 7:16 PM
> Subject: RE: [R] Continuous columns of matrix
>
>
>
>
> o yes!!! i didnt notice..
> you got any clue??
> :(((((
>
>
> > Date: Fri, 24 May 2013 15:58:28 -0700
> > From: smartpink111@yahoo.com
> > Subject: Re: [R] Continuous columns of matrix
> > To: eliza_botto@hotmail.com
> >
> > Hi Elisa,
> > In case you haven't noticed.
> >
> > There is a slight difference in output in the matrix example using
Bill's function and other functions. Probably, you need to tweak the code a
bit.
> > [[4]]
> > Index Value
> > 1 1 2.061959
> > 2 12 2.245474
> >
> >
> >
> >
> >
> > ----- Original Message -----
> > From: eliza botto <eliza_botto@hotmail.com>
> > To: William Dunlap <wdunlap@tibco.com>;
"r-help@r-project.org" <r-help@r-project.org>;
"ruipbarradas@sapo.pt" <ruipbarradas@sapo.pt>
> > Cc:
> > Sent: Friday, May 24, 2013 6:36 PM
> > Subject: Re: [R] Continuous columns of matrix
> >
> > Dear William,
> > You loop worked well in case when i work with only one column. but wat
if i have the following data. how will i do it then?
> > structure(c(0.706461987893674, 0.998391468394261, 0.72402995269242,
1.70874688194537, 1.93906363083693, 0.89540353128442, 0.328327645695443,
0.427434603701202, 0.591932250254601, 0.444627635494183, 1.44407704434405,
1.79150336746345, 0.94525563730664, 1.1025988539757, 0.944726401770203,
0.941068515436361, 1.50874009152312, 0.590015480056925, 0.311905493999476,
0.596771673581893, 1.01502499067153, 0.803273181849135, 1.6704085033648,
1.57021117646422, 0.563879907485297, 0.749077642331436, 0.681023650957497,
1.30140773002346, 1.46377246795771, 1.20312609775816, 0.651886452442823,
0.853749099839423, 1.041608733313, 0.690719733451964, 1.49415144965002,
1.30559703478921, 2.06195904001605, 1.41493262330451, 1.35748791897328,
1.19490680241894, 0.702488756183322, 0.338258418490199, 0.123398398622741,
0.138548982660226, 0.16170889185798, 0.414543218677095, 1.84629295875002,
2.24547399004563, 0.766278117577654, 0.751997501086888, 0.836280758630117,
1.188156460303,
> > 1.56771616670373, 1.5928168139479, 0.522523036011874,
0.561678840701488, 1.11155735914479, 1.26467106348848, 1.09378883406298,
1.17607018089421), .Dim = c(12L, 5L))
> > I used 3rd column in my first question..
> >
> > thanks for the help
> > Elisa
> >
> >
> > > From: wdunlap@tibco.com
> > > To: eliza_botto@hotmail.com; ruipbarradas@sapo.pt;
r-help@r-project.org
> > > Subject: RE: [R] Continuous columns of matrix
> > > Date: Fri, 24 May 2013 22:25:52 +0000
> > >
> > > Are you trying to identify the highest point in each run of
points higher than a threshold,
> > > akin to the problem of naming mountains so that each minor bump
on a high ridge does not
> > > get its own name? The following does that:
> > >
> > > f <- function (x, threshold = 0.8 * max(x), plot=FALSE)
> > > {
> > > if (plot) { plot(x, type="l") ; abline(h=threshold)
}
> > > big <- x > threshold
> > > n <- length(big)
> > > startRunOfBigs <- which(c(big[1], !big[-n] & big[-1]))
> > > if (plot) abline(v=startRunOfBigs, col="green")
> > > endRunOfBigs <- which(c(big[-n] & !big[-1], big[n]))
> > > if (plot) abline(v=endRunOfBigs, col="red")
> > > stopifnot(length(startRunOfBigs) == length(endRunOfBigs),
> > > all(startRunOfBigs <= endRunOfBigs))
> > > index <- vapply(seq_along(startRunOfBigs),
> > >
function(i)which.max(x[startRunOfBigs[i]:endRunOfBigs[i]])+startRunOfBigs[i]-1L,
> > > 0L)
> > > if (plot && length(index)>0) points(cex=1.5,
index, x[index])
> > > data.frame(Index=index, Value=x[index])
> > > }
> > >
> > > E.g., with your data:
> > > > x <- c(0.563879907485297, 0.749077642331436,
0.681023650957497, 1.30140773002346,
> > > 1.46377246795771, 1.20312609775816, 0.651886452442823,
0.853749099839423,
> > > 1.041608733313, 0.690719733451964, 1.49415144965002,
1.30559703478921
> > > )
> > > > f(x, plot=TRUE) # the plot illustrates what it is doing
> > > Index Value
> > > 1 5 1.463772
> > > 2 11 1.494151
> > >
> > > Bill Dunlap
> > > Spotfire, TIBCO Software
> > > wdunlap tibco.com
> > >
> > >
> > > > -----Original Message-----
> > > > From: r-help-bounces@r-project.org
[mailto:r-help-bounces@r-project.org] On Behalf
> > > > Of eliza botto
> > > > Sent: Friday, May 24, 2013 2:10 PM
> > > > To: ruipbarradas@sapo.pt; r-help@r-project.org
> > > > Subject: Re: [R] Continuous columns of matrix
> > > >
> > > > Dear Rui,
> > > > Regarding your last reply there is a small additional
question which i want to ask. For the
> > > > following column
> > > > > dput(mat[,1])c(0.563879907485297, 0.749077642331436,
0.681023650957497,
> > > > 1.30140773002346, 1.46377246795771, 1.20312609775816,
0.651886452442823,
> > > > 0.853749099839423, 1.041608733313, 0.690719733451964,
1.49415144965002,
> > > > 1.30559703478921)
> > > > Your loop gives following results
> > > > [,1] [,2] [,3] [,4]
> > > > index 4.000000 5.000000 6.000000 11.000000
> > > > values 1.301408 1.463772 1.203126 1.494151
> > > > which is very accurate. Index 11 had the maximum value and
4,5,6 were the neibhours. i
> > > > want to add an other condition which is that if a column has
more
> > > > than 1 neibhours, than just like the maximum value, those
neibhour can not be next to
> > > > each other as well.
> > > > So what i should have is the following
> > > > [,1] [,2]
> > > > index 5.000000 11.000000
> > > > values 1.463772 1.494151
> > > > Is there anyway of doing it??
> > > > Thanks in advance
> > > > Elisa
> > > >
> > > > > Date: Fri, 24 May 2013 17:02:56 +0100
> > > > > From: ruipbarradas@sapo.pt
> > > > > To: eliza_botto@hotmail.com
> > > > > CC: bhh@xs4all.nl; r-help@r-project.org
> > > > > Subject: Re: [R] Continuous columns of matrix
> > > > >
> > > > > Hello,
> > > > >
> > > > > No problem. Just change <0 to >= and Inf to -Inf:
> > > > >
> > > > > fun2 <- function(x){
> > > > > n <- length(x)
> > > > > imx <- which.max(x)
> > > > > if(imx == 1){
> > > > > x[2] <- x[n] <- -Inf
> > > > > }else if(imx == n){
> > > > > x[1] <- x[n - 1] <- -Inf
> > > > > }else{
> > > > > x[imx - 1] <- -Inf
> > > > > x[imx + 1] <- -Inf
> > > > > }
> > > > > index <- which(x >= 0.8*x[imx])
> > > > > values <- x[index]
> > > > > list(index = index, values = values)
> > > > > }
> > > > >
> > > > > apply(mat, 2, fun2)
> > > > >
> > > > >
> > > > > Rui Barradas
> > > > >
> > > > > Em 24-05-2013 16:23, eliza botto escreveu:
> > > > > > Dear Rui,
> > > > > >
> > > > > > I infact wanted to have something like the
following..
> > > > > > suppose the columns are
> > > > > >
> > > > > > structure(c(0.706461987893674, 0.998391468394261,
0.72402995269242,
> > > > 1.70874688194537, 1.93906363083693, 0.89540353128442,
0.328327645695443,
> > > > 0.427434603701202, 0.591932250254601, 0.444627635494183,
1.44407704434405,
> > > > 1.79150336746345, 2.06195904001605, 1.41493262330451,
1.35748791897328,
> > > > 1.19490680241894, 0.702488756183322, 0.338258418490199,
0.123398398622741,
> > > > 0.138548982660226, 0.16170889185798, 0.414543218677095,
1.84629295875002,
> > > > 2.24547399004563), .Dim = c(12L, 2L))
> > > > > >
> > > > > > For col 1
> > > > > > [[1]]
> > > > > > $Index
> > > > > > 5 12
> > > > > > $value
> > > > > > 1.939 1.79
> > > > > > Although value 1.708 of index 4 also has value
which is above 80% of the maximum
> > > > value but as it is in the neighbor of maxmimum value so we
wont consider it.
> > > > > > similarly for the column 2
> > > > > > [[1]]
> > > > > > $Index
> > > > > > 12
> > > > > > $value
> > > > > > 2.245
> > > > > > Although values 1.846 of index 11 and 2.0619 of
index 1 also have values which are
> > > > above 80% of the maximum value but as they are in the
neighbor of maxmimum value so
> > > > we wont consider them.
> > > > > > i am sorry if the manner in which i asked my
question was not conclusive.
> > > > > > i hope you wont mind...
> > > > > > Elisa
> > > > > >> Date: Fri, 24 May 2013 15:59:50 +0100
> > > > > >> From: ruipbarradas@sapo.pt
> > > > > >> To: eliza_botto@hotmail.com
> > > > > >> CC: bhh@xs4all.nl; r-help@r-project.org
> > > > > >> Subject: Re: [R] Continuous columns of matrix
> > > > > >>
> > > > > >> Hello,
> > > > > >>
> > > > > >> Something like this?
> > > > > >>
> > > > > >>
> > > > > >> fun2 <- function(x){
> > > > > >> n <- length(x)
> > > > > >> imx <- which.max(x)
> > > > > >> if(imx == 1){
> > > > > >> x[2] <- x[n] <- Inf
> > > > > >> }else if(imx == n){
> > > > > >> x[1] <- x[n - 1] <- Inf
> > > > > >> }else{
> > > > > >> x[imx - 1] <- Inf
> > > > > >> x[imx + 1] <- Inf
> > > > > >> }
> > > > > >> index <- which(x <= 0.8*x[imx])
> > > > > >> values <- x[index]
> > > > > >> list(index = index, values = values)
> > > > > >> }
> > > > > >>
> > > > > >> apply(mat, 2, fun2)
> > > > > >>
> > > > > >>
> > > > > >> Rui Barradas
> > > > > >>
> > > > > >> Em 24-05-2013 13:40, eliza botto escreveu:
> > > > > >>> Dear Rui,Thankyou very much for your help.
just for my own knowledge what if
> > > > want the values and index, which are less than or equal to
80% of the maximum value
> > > > other than those in the neighbors?? like if maximum is in
row number 5 of any column
> > > > then the second maximum can be in any row other than 4 and
6. similarly if maximum is
> > > > in row number 12 than the second maximum can be in any row
other than 1 and
> > > > 11...thankyou very much for your help
> > > > > >>> elisa
> > > > > >>>
> > > > > >>>> Date: Fri, 24 May 2013 12:37:37 +0100
> > > > > >>>> From: ruipbarradas@sapo.pt
> > > > > >>>> To: eliza_botto@hotmail.com
> > > > > >>>> CC: bhh@xs4all.nl;
r-help@r-project.org
> > > > > >>>> Subject: Re: [R] Continuous columns of
matrix
> > > > > >>>>
> > > > > >>>> Hello,
> > > > > >>>>
> > > > > >>>> Berend is right, it's at least
confusing. To get just the index of the
> > > > > >>>> maximum value in each column,
> > > > > >>>>
> > > > > >>>> apply(mat, 2, which.max)
> > > > > >>>>
> > > > > >>>>
> > > > > >>>> To get that index and the two
neighbours (before and after, wraping
> > > > > >>>> around) if they are greater than or
equal to 80% of the maximum, try
> > > > > >>>>
> > > > > >>>> fun <- function(x){
> > > > > >>>> n <- length(x)
> > > > > >>>> imx <- which.max(x)
> > > > > >>>> sec <- numeric(2)
> > > > > >>>> if(imx == 1){
> > > > > >>>> if(x[n] >= 0.8*x[imx])
sec[1] <- n
> > > > > >>>> if(x[2] >= 0.8*x[imx])
sec[2] <- 2
> > > > > >>>> }else if(imx == n){
> > > > > >>>> if(x[n - 1] >= 0.8*x[imx])
sec[1] <- n - 1
> > > > > >>>> if(x[1] >= 0.8*x[imx])
sec[2] <- 1
> > > > > >>>> }else{
> > > > > >>>> if(x[imx - 1] >=
0.8*x[imx]) sec[1] <- imx - 1
> > > > > >>>> if(x[imx + 1] >=
0.8*x[imx]) sec[2] <- imx + 1
> > > > > >>>> }
> > > > > >>>> sec <- sec[sec != 0]
> > > > > >>>> c(imx, sec)
> > > > > >>>> }
> > > > > >>>>
> > > > > >>>> apply(mat, 2, fun)
> > > > > >>>>
> > > > > >>>>
> > > > > >>>> Note that the result comes with the
maximum first and the others follow.
> > > > > >>>>
> > > > > >>>> Hope this helps,
> > > > > >>>>
> > > > > >>>> Rui Barradas
> > > > > >>>>
> > > > > >>>>
> > > > > >>>> Em 24-05-2013 11:41, eliza botto
escreveu:
> > > > > >>>>> There you go!!!
> > > > > >>>>>
> > > > > >>>>> structure(c(0.706461987893674,
0.998391468394261, 0.72402995269242,
> > > > 1.70874688194537, 1.93906363083693, 0.89540353128442,
0.328327645695443,
> > > > 0.427434603701202, 0.591932250254601, 0.444627635494183,
1.44407704434405,
> > > > 1.79150336746345, 0.94525563730664, 1.1025988539757,
0.944726401770203,
> > > > 0.941068515436361, 1.50874009152312, 0.590015480056925,
0.311905493999476,
> > > > 0.596771673581893, 1.01502499067153, 0.803273181849135,
1.6704085033648,
> > > > 1.57021117646422, 0.492096635764151, 0.433332688044914,
0.521585941816778,
> > > > 1.66472272302545, 2.61878329527404, 2.19154489521664,
0.493876245329722,
> > > > 0.4915787202584, 0.889477365620806, 0.609135860199222,
0.739201878930367,
> > > > 0.854663750519518, 0.948228727226247, 1.38569091844218,
0.910510759802679,
> > > > 1.25991218521949, 0.993123416952421, 0.553640392997634,
0.357487763503204,
> > > > 0.368328033777003, 0.344255688489322, 0.423679560916755,
1.32093576037521,
> > > > 3.13420679229785, 2.06195904001605, 1.41493262330451,
1.35748791897328,
> > > > 1.19490680241894, 0.702488!
> > > > 75618332!
> > > > > >>>>> 2, 0.338258418490199,
0.123398398622741, 0.138548982660226,
> > > > 0.16170889185798, 0.414543218677095, 1.84629295875002,
2.24547399004563,
> > > > 0.0849732189580101, 0.070591276171845, 0.0926010253161898,
0.362209761457517,
> > > > 1.45769283057202, 3.16165004659667, 2.74903557756267,
1.94633472878995,
> > > > 1.19319875840883, 0.533232612926756, 0.225531074123974,
0.122949089115578),
> > > > .Dim = c(12L, 6L))
> > > > > >>>>>
> > > > > >>>>> Thanks once again..
> > > > > >>>>> Elisa
> > > > > >>>>>
> > > > > >>>>>
> > > > > >>>>>> Subject: Re: [R] Continuous
columns of matrix
> > > > > >>>>>> From: bhh@xs4all.nl
> > > > > >>>>>> Date: Fri, 24 May 2013
12:36:47 +0200
> > > > > >>>>>> CC: r-help@r-project.org
> > > > > >>>>>> To: eliza_botto@hotmail.com
> > > > > >>>>>>
> > > > > >>>>>>
> > > > > >>>>>> On 24-05-2013, at 12:24, eliza
botto <eliza_botto@hotmail.com> wrote:
> > > > > >>>>>>
> > > > > >>>>>>> Dear useRs,If i have a
matrix, say, 12 rows and 6 columns. The columns are
> > > > continuous. I want to find the index of maximum values and
the actual maximum values.
> > > > The maximum values in each column are the highest values and
the values greater than
> > > > or equal to 80% of the maximum value. Moreover, if a column
has more than one
> > > > maximum values than these values should come immediately
next to each other. For
> > > > example, if you column 1 has a highest value in 6th row then
the second maximum values
> > > > cant be in row 5 or 7. And as the columns are continuous
therefore, if maximum value is
> > > > in row 12th, then the second maximum cant be in row 11 and
1.Thankyou very much
> > > > indeed in advance
> > > > > >>>>>>
> > > > > >>>>>>
> > > > > >>>>>> Incomprehensible.
> > > > > >>>>>> What is a continuous column?
> > > > > >>>>>>
> > > > > >>>>>> Please give an example input
matrix and and the result you want.
> > > > > >>>>>>
> > > > > >>>>>> Berend
> > > > > >>>>>>
> > > > > >>>>>>> Elisa
> > > > > >>>>>>> [[alternative HTML
version deleted]]
> > > > > >>>>>>>
> > > > > >>>>>>
> > > > > >>>>>> Please post in plain text.
> > > > > >>>>>>
> > > > > >>>>>
> > > > > >>>>> [[alternative HTML version
deleted]]
> > > > > >>>>>
> > > > > >>>>>
______________________________________________
> > > > > >>>>> R-help@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.
> > > > > >>>>>
> > > > > >>>
> > > > > >>>
> > > > > >
> > > > > >
> > > >
> > > > [[alternative HTML version deleted]]
> > > >
> > > > ______________________________________________
> > > > R-help@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.
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help@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.
> >
[[alternative HTML version deleted]]