Skip to content

Find the max entry in column 2 - that satisfies a condition given a fixed entry in column 1

5 messages · Vangelis Litos, Bert Gunter, Rui Barradas +1 more

#
I am using R and I created based on a vector x, a grid (named: plain) - where zero is included. This gives me a 9x2 ``matrix''.
My aim is to focus on column 1 and take i.e the first entry (then the second, the third entry etc through a loop) of the unique (c (0, x)) vector (==0) [rows 1, 4 and 7] and find the maximum value (entry) in the second column of the matrix that satisfies a condition.

Let say that the condition is satisfied when at column 2 the value is 2. That is I want row 7 to be selected

Then I want to create a column vector b (9x1) that has zero everywhere apart from row 7.

Can anybody help me with this?

Thank you in advance.
#
"Can anybody help me with this?"

Probably not. Your explanation is as clear as mud -- to me anyway. Showing
us the actual data or code of a simple example, what you want to do and
what you have tried, and what you would like to get would likely help
(assuming it's not just me being dense, always a distinct possibility!).

And please *post in **plain text** not html* to avoid opaque text
formatting in this plain text list.

Cheers,
Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Jun 20, 2019 at 4:50 PM Vangelis Litos <Redondo5_447 at hotmail.com>
wrote:

  
  
#
Hello,

Inline.

?s 10:15 de 20/06/19, Vangelis Litos escreveu:
This code works but there are several issues with it.

1) c(1) is exactly the same as just 1. Try running

identical(c(1), 1)

2) You end the line with a semi-colon. This is not needed.
In fact,

the semi-colon is the INSTRUCTIONS SEPARATOR

so what you end up with is 2 instructions, one on the left of the ; and 
the other, the NULL instruction, on the right. What the parser sees and 
processes is

instruction;NULL

3) Then in the next line you transpose the matrix you have created. Much 
  simpler:

y <- rbind(1, 2)
identical(x, y)    # TRUE
4) Now you combine the matrix you have created with a new element, 0.

c(0, x)

The output of this is no longer a matrix, it's a vector without the dim 
attribute.

identical(c(0, x), c(0, 1, 2))    # TRUE
The following function does this.


fun <- function(X, val){
   u <- sort(unique(X))
   plain <- expand.grid(u, u)
   w <- which(plain[, 1] == val)
   w[which.max(plain[w, 2])]
}

fun(c(0, x), 0)
#[1] 7
fun(c(x, 0, x), 0)
#[1] 7
And what would be the non-zero value? The max in column 2?

b <- numeric(nrow(plain))    # creates a vector of 9 zeros
i <- fun(c(0, x), 0)
b[i] <- new_value

# or maybe

b[i] <- plain[i, 2]


The first and the last instructions have an obvious problem, 'plain' 
only exists inside the function fun(), it will have to be created elsewhere.


Hope this helps,

Rui Barradas
#
Hello,

Thinking again, if all you want is vector b the following function fun2 
will do it without having to create plain. Less memory and (much) faster.

fun2 <- function(X, val){
   u <- sort(unique(X))
   n <- length(u)
   b <- numeric(n^2)
   b[n*(n - 1) + match(val, u)] <- u[n]
   b
}

fun2(c(0, x), 0)
fun2(c(0, x), 1)
fun2(c(0, x), 2)


Hope this helps,

Rui Barradas

?s 06:47 de 21/06/19, Rui Barradas escreveu:
1 day later
#
I have read your message four times in the last couple of days,
but I still have very little idea what you want.
Let's try some things I've gleaned.
You have a matrix with 9 rows and 2 columns, and there is a 2
somewhere in column 1.
[,1] [,2]
 [1,]    1   10
 [2,]    2   11
 [3,]    3   12
 [4,]    2   13
 [5,]    5   14
 [6,]    6   15
 [7,]    2   16
 [8,]    2   17
 [9,]    9   18
You want to select rows where column 1 is 2.
[1] FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE  TRUE FALSE
You want to select the corresponding elements from column 2.
[1] 11 13 16 17
You want the maximum of these elements.
[1] 17

You can do pretty amazing things with indexing in R without any extra
packages.

Of course, I have almost certainly misunderstood you.
Perhaps you can ask again, maybe with an example?

On Fri, 21 Jun 2019 at 11:50, Vangelis Litos <Redondo5_447 at hotmail.com>
wrote: