简体   繁体   中英

subset dataframe, based on min()

I am trying to establish a function, that uses y argument to subset a dataframe and then calculate the min of the yp column. The minimum of yp column is then used to subset the same dataframe in regard to column c .

b <- function (y) {
    df<- read.csv("C:/../.csv", colClasses="character")
    y.p<-paste("d",y, sep=".")
    minimum=min(min.outcome<-as.numeric(data.frame[,y.p]),na.rm=T)
    df[df$y.p==minimum,"c"]
}

here is a part of the dataframe: https://www.dropbox.com/s/y3152d1ki1ot232/Classeur2.csv

After running the function, I am getting character(0) as result !

I have tested the function line by line and the error seems to be in the last line..

May you help me solve this issue plz ?

Thanks,

Okay well to be honest it's hard to tell what exactly you are trying to accomplish, but I think you are looking to be able to pass in a character value to y in the event that the data you are reading in has multiple columns beginning with d. ? Anyways, if this is incorrect, please walk through what you are trying to do step by step.

Foo <- function(y){
  df <- read.csv(
    file="G:/Classeur2.csv",
    header=TRUE,
    colClasses=c(
      'numeric',
      rep('character',4))
  )
  ##
  df[,3:5] <- sapply(3:5, function(X){
    df[,X] <- as.numeric(
      gsub("Not Available",NA,df[,X])
    ) 
  })
  ##
  y.p <- paste("d",y,sep=".")
  ypCol <- match(y.p,names(df))
  ##
  minimum <- min(
    df[,ypCol],
    na.rm=TRUE
  )
  ##
  dOut <- df[df[,ypCol]==minimum,"c"]
  dOut <- dOut[!is.na(dOut)]
  return(dOut)
}
##
Foo(y="y")
## [1] "HOSPITAL SYSTEM"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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