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?
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.
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.
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.
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)
If you prefer using column names, you could do something like this as an alternative:
min(data$column_name)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With