使用数字值

时间:2017-08-22 20:29:30

标签: r

我有一个看起来像这样的矩阵:

canada <- c(10, 20, 90)
korea <- c(60, 30, 90)
iran <- c(20, 20, 40)
fruit <- rbind(canada, korea, iran)
colnames(fruit) <- c("apple", "banana", "orange")

我希望它看起来像这样:

price("canada")
> Canada has affordable fruits, but buying an orange is expensive. They cost 90 cents each.

所以,感谢StackOverflow的一些人,我有这个:

price <- function(val){
  val <- tolower(val) # assuming all row names are in lower case
  myrow <- fruit[val,]
  nation <- tools::toTitleCase(val)

  # Apple Prices
  apple <- sapply(seq_along(myrow), 
                      function(x, n, i) {paste0(x[i], ".")}, 
                      x=myrow[1], n=names(myrow))
  apple[length(apple)] <- paste0(apple[length(apple)])
  apple <- paste(apple[1])

  # Banana Prices
  banana <- sapply(seq_along(myrow), 
                   function(x, n, i) {paste0(x[i], ".")}, 
                   x=myrow[2], n=names(myrow))
  banana[length(banana)] <- paste0(banana[length(banana)])
  banana <- paste(banana[1])

  # Orange Prices
  orange <- sapply(seq_along(myrow), 
                   function(x, n, i) {paste0(x[i], ".")}, 
                   x=myrow[3], n=names(myrow))
  orange[length(orange)] <- paste0(orange[length(orange)])
  orange <- paste(orange[1])

  if(myrow[1] > 30 | myrow[2] > 30 | myrow[3] > 30)
    cat("This is my limit, and I need to push myself over the edge to learn more.")
}

price("canada")

我的想法是,我可以选择多种水果,并能够提取一些价格昂贵的水果,并说“#34; ...但是,((这里的水果名称))价格昂贵。” #34;如果我能提到昂贵的水果的价格也会很好,但我会满意的只是提到那些昂贵的水果的名称。

3 个答案:

答案 0 :(得分:1)

您可以使用此简化功能,它只使用which.max

抓取最昂贵的项目
price <- function(val){
             val <- tolower(val) # assuming all row names are in lower case
             myrow <- fruit[val,]
             nation <- tools::toTitleCase(val)

             paste0(nation, " has affordable fruits, but buying an ", names(myrow)[which.max(c(myrow))], " is expensive. They cost ", c(myrow)[which.max(c(myrow))]," cents each.")
         }

price("canada")

输出

"Canada has affordable fruits, but buying an orange is expensive. They cost 90 cents each."

打印超过阈值的水果

price <- function(val,threshold){
             val <- tolower(val) # assuming all row names are in lower case
             myrow <- fruit[val,]
             nation <- tools::toTitleCase(val)

             paste0(nation, " has affordable fruits, but buying an ", paste0(names(myrow)[c(myrow)>threshold], collapse=" and "), " is expensive. They cost ", paste0(c(myrow)[c(myrow)>threshold], collapse=" and ")," cents each.")
     }

 price("korea",30)

输出

[1] "Korea has affordable fruits, but buying an apple and orange is expensive. They cost 60 and 90 cents each."

答案 1 :(得分:1)

如果水果价格至少比最贵的水果贵2/3,那么这种水果的选择价格便宜。措辞有点不同(“橙子”代替“橙子”)以允许多种水果。您可以将2/3 max(fruit.prices)行更改为对您最有意义的行。它也可以是固定值。

price <- function(val){
    val <- tolower(val) # assuming all row names are in lower case
    myrow <- fruit[val,]
    nation <- tools::toTitleCase(val)

    fruit.prices <- fruit[val,]
    high.value <- 2/3 * max(fruit.prices)

    expensive.fruits <- fruit.prices[fruit.prices >= high.value]
    efruit.names <- paste0(names(expensive.fruits), "s", 
                               collapse = " and ")
    efruit.prices <- paste0(expensive.fruits, collapse = " and ")
    paste(nation, "has has affordable fruits, but buying", efruit.names,
          "is expensive. They cost", efruit.prices, "cents each.")

}

输出:

> price("canada")
[1] "Canada has has affordable fruits, but buying oranges is expensive. They cost 90 cents each."
> price("korea")
[1] "Korea has has affordable fruits, but buying apples and oranges is expensive. They cost 60 and 90 cents each."
> price("iran")
[1] "Iran has has affordable fruits, but buying oranges is expensive. They cost 40 cents each."
> 

答案 2 :(得分:1)

我会处理一个data.frame,例如我在评论中建议的那个。

nu.fruit <- do.call(rbind, lapply(1:ncol(fruit), (function(i){
  do.call(rbind, lapply(1:nrow(fruit), (function(j){
    data.frame(country=rownames(fruit)[j], 
      fruit=colnames(fruit)[i],
      price=fruit[j,i])  
  })))  
})))

我的解决方案将基于您可以设置的阈值。这是功能。

is.pricey <- function(country, data){

  # check arguments
  if ((!is.data.frame(data)) |
      (sum(c("country", "fruit", "price") %in% colnames(data)) != 3) |
      (length(country) != 1))
    stop("Bad input!")

  # retrieve cheapest fruit
  country <- tolower(country)
  tmp <- data[data[,"country"]== country,]
  if (nrow(tmp) > 0){
    my.max <- max(tmp[,"price"])
    return(tmp[tmp[,"price"] == my.max, ])
  } else {
    return(NULL)
  }
}

what.is.pricey <- function(country, data, threshold) {

  # check args
  if ((!is.numeric(threshold)) |
      (length(threshold) != 1))
    stop("Bad input!")

  # retrieve expensive items
  exp.stuff <- is.pricey(country,data)

  # check you have data and if they meet your conditions
  if (!is.null(exp.stuff)) {
    exp.stuff <- exp.stuff[exp.stuff[,"price"] >= threshold,]
    if (nrow(exp.stuff) > 0) {
      return(paste("Living in",
                   country,
                   "is cheap, but buying a",
                   exp.stuff$fruit, 
                   "costs", 
                   exp.stuff$price[1], collapse = ", "))   
    }
  }
}

这里是电话

what.is.pricey(country = "korea", data = nu.fruit, threshold = 40)