' =='列标题的运算符?

时间:2017-08-24 01:20:38

标签: 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 <- function(val){
  val <- tolower(val) # assuming all row names are in lower case
  myrow <- fruit[val,]
  nation <- tools::toTitleCase(val)

  name.min <- names(myrow)[which.min(c(myrow))]
  score.min <- c(myrow)[which.min(c(myrow))]
  cat(paste0("In ", nation, ", an ", name.min, " is ", score.min, " cents. ", "\n\n"))

  name.max <- names(myrow)[which.max(c(myrow))]
  score.max <- c(myrow)[which.max(c(myrow))]
  cat(paste0("In ", nation, ", an ", name.max, " is ", score.max, " cents. ", "\n\n"))
  }

以上脚本导致:

> price("canada")
In Canada, an apple is 10 cents. 

In Canada, an orange is 90 cents. 

我想知道经济实惠的苹果是否来自加拿大,所以我补充说:

if(name.min == myrow[1])
  cat("If name.min is 'apple', then it should print. But, it does not?",
      "Affordable apples come from", nation, ".")
if(name.min != myrow[1])
  cat("If name.max is not 'apple', then it should print. And, it does.")

总的来说,完整的脚本是:

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 <- function(val){
  val <- tolower(val) # assuming all row names are in lower case
  myrow <- fruit[val,]
  nation <- tools::toTitleCase(val)

  name.min <- names(myrow)[which.min(c(myrow))]
  score.min <- c(myrow)[which.min(c(myrow))]
  cat(paste0("In ", nation, ", an ", name.min, " is ", score.min, " cents. ", "\n\n"))

  name.max <- names(myrow)[which.max(c(myrow))]
  score.max <- c(myrow)[which.max(c(myrow))]
  cat(paste0("In ", nation, ", an ", name.max, " is ", score.max, " cents. ", "\n\n"))

  if(name.min == myrow[1])
    cat("If name.min is 'apple', then it should print. But, it does not?",
        "Affordable apples come from", nation ".")
  if(name.min != myrow[1])
    cat("If name.max is not 'apple', then it should print. And, it does.")

  }

打印以下内容:

> price("canada")
In Canada, an apple is 10 cents. 

In Canada, an orange is 90 cents. 

If name.max is not 'apple', then it should print. And, it does.

我的推测是第一个选项 if(name.min == myrow [1])不会打印,因为它正在将字母与数字进行比较。第二个选项 if(name.min!= myrow [1] 打印,因为字母不是数字。

我在这里做出正确的推测吗?如果是这样,替换myrow会有什么替代方法[1]?

2 个答案:

答案 0 :(得分:1)

您的目标是找出生产价格实惠的水果类型的国家:

我为你写了一个简单的代码:

price <- function(val){

  val <- tolower(val) # assuming all row names are in lower case
  myrow <- fruit[,val]

  nations <- names(myrow)[which(myrow==min(myrow))]

  #Icould have used which.min, but that will give me just one country in   case there
  #are more than one country which has affordable  val.
  art = ifelse( grepl("[aeiou]",substring(val,1,1)),"an","a")
       #This is the article to be placed before the fruit ie "an" or "a"

  nations <- tools::toTitleCase(nations)

  cat(paste0("In ",tools::toTitleCase(names(myrow))," ",
         art," ",val," is ",myrow, " cents.","\n\n"),sep = "")

  if(length(nations)>1) nations = paste(nations,collapse = " and ")
   cat( "Affordable",paste0(val,"s"),"come from", nations, ".")

}

输出:

 price("apple")
 In Canada an apple is 10 cents.

 In Korea an apple is 60 cents.

 In Iran an apple is 20 cents.

 Affordable apples come from Canada.

 price("banana")
 In Canada a banana is 20 cents.

 In Korea a banana is 30 cents.

 In Iran a banana is 20 cents.

 Affordable bananas come from Canada and Iran.

 price("orange")
 In Canada an orange is 90 cents.

 In Korea an orange is 90 cents.

 In Iran an orange is 40 cents.

 Affordable oranges come from Iran.

答案 1 :(得分:0)

按照Onyambu的提示,我想我可能找到了答案:

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 <- function(val){
  val <- tolower(val) # assuming all row names are in lower case
  myrow <- fruit[val,]
  nation <- tools::toTitleCase(val)

  name.min <- names(myrow)[which.min(c(myrow))]
  score.min <- c(myrow)[which.min(c(myrow))]
  cat(paste0("In ", nation, ", an ", name.min, " is ", score.min, " cents. ", "\n\n"))

  name.max <- names(myrow)[which.max(c(myrow))]
  score.max <- c(myrow)[which.max(c(myrow))]
  cat(paste0("In ", nation, ", an ", name.max, " is ", score.max, " cents. ", "\n\n"))

  if(name.min == names(myrow[1]))
    cat("If name.min is 'apple', then it should print.",
        "Affordable apples come from", nation, ".", "\n\n")
  if(name.min != names(myrow)[which.min(c(myrow))])
    cat("If name.max is not 'apple', then it should print. And, it does.")

  }

非常感谢你指出我正确的方向。

> price("canada")
In Canada, an apple is 10 cents. 

In Canada, an orange is 90 cents. 

If name.min is 'apple', then it should print. Affordable apples come from Canada .