# [R] Continuous columns of matrix

Sat May 25 00:38:51 CEST 2013

```Hello,

Just use

apply(mat, 2, f)

Em 24-05-2013 23:25, William Dunlap escreveu:
> 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])
> }
>
>> 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 at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
>> Of eliza botto
>> Sent: Friday, May 24, 2013 2:10 PM
>> To: ruipbarradas at sapo.pt; r-help at r-project.org
>> Subject: Re: [R] Continuous columns of matrix
>>
>> Dear Rui,
>> 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??
>> Elisa
>>
>>> Date: Fri, 24 May 2013 17:02:56 +0100
>>> To: eliza_botto at hotmail.com
>>> CC: bhh at xs4all.nl; r-help at 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)
>>>
>>>
>>>
>>> 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
>>>>> To: eliza_botto at hotmail.com
>>>>> CC: bhh at xs4all.nl; r-help at 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)
>>>>>
>>>>>
>>>>>
>>>>> 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
>>>>>>> To: eliza_botto at hotmail.com
>>>>>>> CC: bhh at xs4all.nl; r-help at 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,
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> 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 at xs4all.nl
>>>>>>>>> Date: Fri, 24 May 2013 12:36:47 +0200
>>>>>>>>> CC: r-help at r-project.org
>>>>>>>>> To: eliza_botto at hotmail.com
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> On 24-05-2013, at 12:24, eliza botto <eliza_botto at 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
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> 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 at r-project.org mailing list
>>>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>>>>
>>>>>>
>>>>>>
>>>>
>>>>
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help