【问题标题】:Why cbind just shows the last row added to the data.frame in R?为什么 cbind 只显示添加到 R 中 data.frame 的最后一行?
【发布时间】:2014-04-15 23:56:36
【问题描述】:

我编写了一个函数,它获取用户预算和购物清单,并提示用户输入她想要的商品数量。这个功能目前有两个问题。当我以 20 作为预算进行测试时,我知道我应该能够购买 10 个菠菜,但它只允许我购买 1 个菠菜。这很奇怪,因为我可以正确购买其余物品。第二个问题是我想通过向其添加行来显示已购买项目的 data.frame。但是只显示最后购买的商品!你知道如何解决这两个问题吗?

how.many<-function(fruit, number){
  string<-paste("How many",fruit,"?",sep=" ")
  fruit.number<-readline(string)
  print("fruit.number")
  print(fruit.number)
  print("number")
  print(number)
  while(fruit.number > number){
    print("ERROR: too many for the budget")
    string<-paste("How many",fruit,"?",sep=" ")
    fruit.number<-readline(string)
  }
  return(as.numeric(fruit.number))
}

grocery.list <- function(file,budget) {
  item.count<-0
  updated.price=budget
  #purchased.items <- data.frame(count= numeric(0), item= character(0), price = numeric(0),quantity=numeric(0))
  purchased.items <- data.frame(count= numeric(0), item= character(0), price = numeric(0),quantity=numeric(0))
  outf<-read.csv(file,header=FALSE)
  colnames(outf)<-c("item","price")
  mat = as.matrix(outf)
  colnames(mat) <- NULL

  for (i in 1:dim[1])
    {
     print(mat[i,2])
     print(updated.price/as.numeric(mat[i,2]))
     number=how.many(mat[i,1],updated.price/as.numeric(mat[i,2]))
     print(number)
     updated.price=updated.price- (number* (as.numeric(mat[i,2])) )
     print(updated.price)

     if (number>0){
       item.count=item.count+1
       purchased.items<-cbind(item.count,mat[i,1],as.numeric(mat[i,2]),number)
     }

     if (updated.price<=0){
       break
     }

    }
  colnames(purchased.items)=c(" ","item","price", "quantity" )

  return(purchased.items )

}

n=grocery.list("groceries.csv",20)
print(n)

所以 how.many 函数中的 while 循环不适用于这里的菠菜!我真的不明白为什么,因为它适用于其余的项目。

这是 groceries.csv 的示例:

spinach,2.00
rice,3.00
toilet paper,4.00
bread,2.40
milk,3.10
apple,0.40

所以这是我得到的结果列表:

> source("grocery.R")
[1] "2.0"
[1] 10
How many spinach ?1
[1] "fruit.number"
[1] "1"
[1] "number"
[1] 10
[1] 1
[1] 18
[1] "3.0"
[1] 6
How many rice ?3
[1] "fruit.number"
[1] "3"
[1] "number"
[1] 6
[1] 3
[1] 9
[1] "4.0"
[1] 2.25
How many toilet paper ?4
[1] "fruit.number"
[1] "4"
[1] "number"
[1] 2.25
[1] "ERROR: too many for the budget"
How many toilet paper ?2
[1] 2
[1] 1
[1] "2.4"
[1] 0.4166667
How many bread ?5
[1] "fruit.number"
[1] "5"
[1] "number"
[1] 0.4166667
[1] "ERROR: too many for the budget"
How many bread ?1
[1] "ERROR: too many for the budget"
How many bread ?0
[1] 0
[1] 1
[1] "3.1"
[1] 0.3225806
How many milk ?2
[1] "fruit.number"
[1] "2"
[1] "number"
[1] 0.3225806
[1] "ERROR: too many for the budget"
How many milk ?1
[1] "ERROR: too many for the budget"
How many milk ?0
[1] 0
[1] 1
[1] "0.4"
[1] 2.5
How many apple ?0
[1] "fruit.number"
[1] "0"
[1] "number"
[1] 2.5
[1] 0
[1] 1
         item           price quantity
[1,] "3" "toilet paper" "4"   "2"

【问题讨论】:

    标签: r function loops while-loop


    【解决方案1】:
    how.many<-function(fruit, number){
      string<-paste("How many",fruit,"?",sep=" ")
      fruit.number<-as.numeric(readline(string))
      while(fruit.number > number){
        print("ERROR: too many for the budget")
        string<-paste("How many",fruit,"?",sep=" ")
        fruit.number<-as.numeric(readline(string))
      }
      return(as.numeric(fruit.number))
    }
    
    grocery.list <- function(file,budget) {
      item.count<-0
      updated.price=budget
      final.price<-0
      purchased.items <- data.frame(count= numeric(0), item= character(0), price = numeric(0),quantity=numeric(0))
      outf<-read.csv(file,header=FALSE)
    
      mat = as.matrix(outf)
      colnames(mat) <- NULL
      m.dim=dim(mat)
      for (i in 1:m.dim[1])
      {
    
        if (updated.price-as.numeric(mat[i,2])>0)
        {
          number=how.many(mat[i,1],updated.price/as.numeric(mat[i,2]))
          updated.price=updated.price- (number* (as.numeric(mat[i,2])) )
          if (number>0){
            item.count=item.count+1
            new_row=data.frame(item=mat[i,1],price=as.numeric(mat[i,2]),quantity=number)
            purchased.items<-rbind(purchased.items,new_row)
          }
    
        }
    
      }
      colnames(purchased.items)=c("item","price", "quantity" )
      print(purchased.items)
    
      print(purchased.items)
      p.mat=as.matrix(purchased.items)
      colnames(p.mat)<-NULL
      rownames(p.mat)<-NULL
      p.dim<-dim(p.mat)
      for (i in 1:p.dim[1])
        final.price<- final.price+ (as.numeric(p.mat[i,2]))*(as.numeric(p.mat[i,3]))
    
      return(final.price)
    
    }
    
    gl=grocery.list("groceries.csv",10)
    string<-paste("Your bill is $",gl,sep="")
    print(string)
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2021-05-18
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      相关资源
      最近更新 更多