matrix - Rows containing minimum value of a column in R -


i work vectors , matrices in r.

in matrix, want return rows has minimum value of particular column (for example, 9th column). values of column can "not available" also. how can this?

to check not available values tried data <- data[data[,9] != "not available"] didn't give me result expected.

for example -

code  name     number 1     india     2.3 2     america   3.5 3     china   not available 4     europe    1.2 5    japan      1.2 

i want rows has minimum value of column number. required output

    code  name     number      4    europe    1.2      5   japan      1.2 

not available values have neglected

by having 'non-numeric' element in column, column converted 'character' or 'factor' class when read data read.csv/read.table. if use stringsasfactors=false, column class 'character', otherwise 'factor' default.

suppose read dataset stringsasfactors=false, can change column 'numeric' as.numeric, non-numeric elements coerced na.

 df1$number <- as.numeric(df1$number) 

if column 'factor' class, may need as.numeric(as.character(df1$number)). subset dataset 'min' value of "number" column.

 subset(df1, number==min(number, na.rm=true))  #    code   name number  #4    4 europe    1.2  #5    5  japan    1.2 

this done without changing class of 'number' (i.e. keeping 'character' column, not recommended)

 subset(df1, number== min( as.numeric(number[number!='not available'])))  #   code   name number  #4    4 europe    1.2  #5    5  japan    1.2 

the best option read dataset specifying "not available" in na.strings , converted na.

 df1 <- read.table('yourfile.txt', header=true, na.strings='not available') 

data

 df1 <- structure(list(code = 1:5, name = c("india", "america", "china",   "europe", "japan"), number = c("2.3", "3.5", "not available",   "1.2", "1.2")), .names = c("code", "name", "number"),  class =   "data.frame", row.names = c(na, -5l)) 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -