MacUser
MacUser

Reputation: 491

Conditional indexing in R

As a working example : a data frame df consists of two numerical columns: x, y I wish to index the value in column y based on the max value in column x

x<-rnorm(100,1, 2)
y<-rnorm(100,0.5, 2)

df<-data.frame(x,y)
max(x)
#[1] 5.842416

how to identify the value of (y) based on the row position of (x) in the data frame and not based on the value of (x)?

The code is implemented in a for loop and this is my solution using dplyr however it returns a column of NAs without errors

x[i] <- max(df$x)
y[i]<-df %>% group_by(y) %>% slice(which.max(x[i]))  

Upvotes: 0

Views: 2238

Answers (2)

Connor Krenzer
Connor Krenzer

Reputation: 489

If I understand the question correctly, all you have to do is filter the data frame by x and then pull out the value(s) in y, as shown below:

set.seed(2021)
x <- rnorm(100,1, 2)
y <- rnorm(100,0.5, 2)
df <- data.frame(x,y)

df[df$x == max(df$x), "y"]
#> -1.398957

If you insist on using the tidyverse:

df %>% 
  filter(x == max(x)) %>% 
  pull(y)
#> -1.398957

If you don't need a data frame, you can try:

y[x == max(x)]
#> -1.398957

The key to this is logical subsetting--the stuff R is good at!

Upvotes: 2

Nicol&#225;s Velasquez
Nicol&#225;s Velasquez

Reputation: 5898

Try

df[which(df$x == max(df$x)), "y"]

Upvotes: 2

Related Questions