简体   繁体   English

子集数据帧,基于min()

[英]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. 我正在尝试建立一个函数,该函数使用y参数对数据框进行子集设置,然后计算yp列的最小值。 The minimum of yp column is then used to subset the same dataframe in regard to column c . 然后,将yp列的最小值用于关于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 这是数据框的一部分: https : //www.dropbox.com/s/y3152d1ki1ot232/Classeur2.csv

After running the function, I am getting character(0) as result ! 运行该函数后,结果是character(0)

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. 好的,说实话,很难说出要完成的任务,但是我认为,如果要读取的数据具有以d.开头的多列,则希望将字符值传递给yd. ? 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"

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

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