Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

value of one column based on value of another column in R

Tags:

I have to find value minimum value in one column and based on that value I have to find the value corresponding in another column.

my data set is K and i have column name as X, Y:

> K
   X  Y
1  2  3
2  4  5
3  6  7
4  8  9
5 10 11

The columns have these values and i find the minimum value of column Y using

apply(K[c(2)],2,min) # this gives me 3.

now i have to relate it to column X which i am finding difficult.

I am totally new to R and i am still learning. Also i don't know any other libraries other than, library(readr).

like image 383
Prashant Kumar Avatar asked Jul 15 '18 14:07

Prashant Kumar


1 Answers

Assuming that you want to get the entry in X which is placed in the same row as the minimum of Y, you can try:

# create example data.frame:
K <- data.frame(X = seq(2, 10, 2), Y = seq(3, 11, 2))

# find index of minimum entry in column Y:
idx <- match(min(K$Y), K$Y) # gives you the first entry of the minimum
idx2 <- which(min(K$Y) %in% K$Y) # gives you all indices of the minimum

# output the corresponding element (or elements) in column X:
K$X[idx]
K$X[idx2]

Regarding the second part of your question, Datacamp has a free introductory course to R covering the very basics.

like image 108
apitsch Avatar answered Sep 28 '22 17:09

apitsch