How to find the minimum value of a column in R?

26

I am new in R and I am trying to do something really simple. I had load a txt file with four columns and now I want to get the minimum value of the second column. This is the code that I have:

 ## Choose the directory of the file

 setwd("//Users//dkar//Desktop")

 ## Read the txt file

 data<-read.table("export_v2.txt",sep="",header=T)

 str(data)

 ##  this command gives me the minimum for all 4 columns!!
 a<-apply(data,2,min)

Actually, if I want to do something like this: min (data(:,2)). But I don't know how to do it in R. Any help?

This question is tagged with r

~ Asked on 2012-12-07 11:40:04

The Best Answer is


49

If you need minimal value for particular column

min(data[,2])

Note: R considers NA both the minimum and maximum value so if you have NA's in your column, they return: NA. To remedy, use:

min(data[,2], na.rm=T)

~ Answered on 2012-12-07 11:42:24


11

If you prefer using column names, you could do something like this as an alternative:

min(data$column_name)

~ Answered on 2015-11-16 20:21:59


Most Viewed Questions: