R 由于缺少值,which.min和which.max的名称输出相同

R 由于缺少值,which.min和which.max的名称输出相同,r,R,我有韩国的以下数据,两列之间缺少一个值: canada <- c(100, 0, 100) korea <- c(100, "", 100) brazil <- c(100, 90, 100) fruit <- rbind(canada, korea, brazil) colnames(fruit) <- c("apple", "orange", "banana") fruit 我希望它看起来像这样: > price("korea") There is an

我有韩国的以下数据,两列之间缺少一个值:

canada <- c(100, 0, 100)
korea <- c(100, "", 100)
brazil <- c(100, 90, 100)
fruit <- rbind(canada, korea, brazil)
colnames(fruit) <- c("apple", "orange", "banana")
fruit
我希望它看起来像这样:

> price("korea")
There is an NA between two columns? No problem: apple banana

这很难看,但应该满足您的要求:

price <- function(x) { 
    temp <- data.frame(val = as.numeric(fruit[rownames(fruit)==x,]),
                    name = colnames(fruit))
    ind <- which(is.na(temp[,1]))
    if (length(ind!=0)) temp <- temp[-ind,]
    temp[order(temp[,1]),2]
}

price("korea")  

搜索以价格为字符的最小值和最大值对我来说似乎也是一个问题-而且韩国的两个可用值无论如何都是相同的,因此min=maxchange为NA以定义缺少的值,并将韩国的一个值修改为100到50以避免并列,这将起作用。。。问题并不在于缺少的值,而是如何将缺少的值=定义为字符,将整个矩阵转换为字符谢谢您花时间阅读我的问题。我非常感激。是的,将100改为50会有效,但我必须在两个分数相同的情况下工作。
> price("korea")
There is an NA between two columns? No problem: apple banana
price <- function(x) { 
    temp <- data.frame(val = as.numeric(fruit[rownames(fruit)==x,]),
                    name = colnames(fruit))
    ind <- which(is.na(temp[,1]))
    if (length(ind!=0)) temp <- temp[-ind,]
    temp[order(temp[,1]),2]
}

price("korea")  
price <- function(val){
  val <- tolower(val)
  myrow <- fruit[val,]
  nation <- tools::toTitleCase(val)

  name.min <- names(myrow)[which.min(c(myrow))]
  name.max <- names(which(myrow == max(myrow)))

  cat(paste0(name.max))
} 

> price("korea")
apple banana