Alvin
Alvin

Reputation: 67

How to print the column when another column met the condition

Example Data

a <- c(1,2,2,3)
b <- c(1,2,3,4)
dat <- data.frame(a,b)

I would like to print the column 2 when any data from the column 1 is >=2

which(dat[,1]>=2)

This only show which row of column2 is greater than 2. I expect it will show:

[1] 2 3 4

Sorry for my bad English and hope you can understand it.

Upvotes: 1

Views: 930

Answers (1)

akrun
akrun

Reputation: 887148

If we need the corresponding values in 2nd column, use the [

dat[,2][dat[,1]>=2]
#[1] 2 3 4

Upvotes: 1

Related Questions