简体   繁体   English

min()无法按预期工作

[英]min() does not work as expected

I am trying to get the minimum of aa column. 我试图得到一个列的最小值。

The data has been split into groups using the "abbr" factor. 数据已使用“abbr”因子分组。 My objective is to return the data in column 2 corresponding to the minimum in column number passed in the argument. 我的目标是返回第2列中与参数中传递的最小列数相对应的数据。 If it helps , this is a part of the coursera R programming introductory course. 如果有帮助,这是课程R编程入门课程的一部分。

The minimum is supposed to be somewhere around 8, it shows 10. 最小值应该在8左右,显示10。

Please help me here. 请帮帮我。

here's the link to the csv file on which i used read.csv 这是我使用read.csv的csv文件的链接

https://drive.google.com/file/d/0Bxkj3-FNtxqrLW14MFZCeEl6UGc/view?usp=sharing https://drive.google.com/file/d/0Bxkj3-FNtxqrLW14MFZCeEl6UGc/view?usp=sharing

best <- function(abbr, outvar){

    ## outcome is a dataframe consisting of a column labelled "State" (one of many)
    ## outvar is the desired column number

    statecol <- split(outcome, outcome$State) ##state is a factor which will be inputted as abbr

    dislist <- statecol[[abbr]][,2][statecol[[abbr]][, outvar] == 
                        min(statecol[[abbr]][, outvar])] ##continuation of prev line
    dislist
}

In my opinion you are messing up with NA, make sure to specify na as not available and na.rm=TRUE in min.. 在我看来你搞砸了NA,确保指定na为不可用,并且na.rm = TRUE in min ..

filedata<-read.table(file.choose(),quote='"',sep=",",dec=".",header=TRUE,stringsAsFactors=FALSE, na.strings="Not Available")
   f<-function(df,abbr,outVar,na.rm=TRUE){
      outlist<-split(df,df["State"])
      tempCol<-outlist[[abbr]][outVar]
      outlist[[abbr]][,2][which(tempCol==min(tempCol,na.rm=na.rm))]
    }
    f(filedata,"AK",44)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM