使用if语句和for循环时R生成错误

时间:2013-06-28 16:08:26

标签: r if-statement for-loop

以下是数据框

   brand  production_cost  sell
   A      220               3
   B      180               1
   C      200               2
   D      240               4
   E      270               7
   F      200               4

如果sell > 3investment = sell * production_cost

如果sell < 3investment = sell * 0.5 * production_cost(生产成本的50%)

我尝试过以下方式:

   data <- read.table("Z:\\who.txt",header=TRUE)

   investment <- c(1,1,1,1,1,1)

  for(i in 1:6){
    if(data$sell[i]>3){
      investment[i] <- sell[i] * production_cost
    }else {
      investment[i] <- sell[i] * 0.5 * production_cost
    }
  } # end for loop

但错误是找不到对象sell

然后我必须计算

如果investment >= 800produce = 1

如果investment < 800produce = 0

虽然我无法计算变量投资,但我认为它是[通过使用计算器]

   investment <- c(330,90,200,960,1890,800)
   produce <- cut(investment,c(-Inf,800,Inf),labels=c("0","1"))

这里的问题是investment[6]=800。我的尝试是将其标记为1。但它标记为0.

接下来,我必须找到produce=1的品牌数量。

我通过以下方式尝试了这个:

  sum=0

  for(i in 1:6){
    if(produce[i]==1)sum=sum+1
  } # end for loop     

这是正确的程序吗?有更好的方法吗?

2 个答案:

答案 0 :(得分:1)

在中使用,它会创建一个环境并返回一个新的数据框:

newdata = within(data, {
investment = ifelse(sell > 3, sell * production_cost, sell * production_cost *0.5 )
})

newdata = within(newdata, {
    produce = ifelse(investment >= 800, 1, 0)
})

注意:此代码设置

 investment = sell * production_cost * 0.5

如果卖== 3

希望它有所帮助。

答案 1 :(得分:0)

假设您的数据框为sample。以下代码未经过测试

#You can use `ifelse` for first two problem

 sample$investment<-with(sample, ifelse(sell>3,sell * production_cost,sell * 0.5 * production_cost))
 sample$produce<-with(sample,ifelse(investment>=800,1,0))

#subset the sample with produce equal to 1 for part 3 and then use ddply from plyr to #count the number of brands

samplesub<-subset(sample, produce==1)

#number of brands 

install.packages("plyr")
library(plyr)
num_brand<-ddply(samplesub, .(brand), summarize, freq=length(brand))

#Alternative to `ddply` from plyr package
#Rather than using the `plyr` package, you can use the following simple code for part 3
num_brand<-with(samplesub,table(brand))