0

下面是一个数据框

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

如果sell > 3那时investment = sell * production_cost

如果sell < 3那么investment = 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 >= 800那时produce = 1

如果investment < 800那时produce = 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     

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

4

2 回答 2

1

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

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

希望能帮助到你。

于 2013-06-28T16:34:42.850 回答
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))
于 2013-06-28T16:32:10.233 回答