Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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?

like image 995
user1919 Avatar asked Dec 07 '12 11:12

user1919


People also ask

How do you find the minimum of a column in R?

Minimum value of a column in R can be calculated by using min() function. min() Function takes column name as argument and calculates the Minimum value of that column.

How do I find the maximum and minimum of a column in R?

min() function in R computes the minimum value of a vector or data frame. max() function in R computes the maximum value of a vector or data frame. column wise maximum and minimum of the dataframe using max() and min() function. Row wise maximum and minimum of the dataframe in R using max() and min() function.

How do you find the minimum and maximum value in R?

We can find the minimum and the maximum of a vector using the min() or the max() function. A function called range() is also available which returns the minimum and maximum in a two element vector.


2 Answers

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) 
like image 111
Didzis Elferts Avatar answered Sep 20 '22 04:09

Didzis Elferts


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

min(data$column_name) 
like image 29
IvFrozen Avatar answered Sep 19 '22 04:09

IvFrozen