The following is a data frame
brand production_cost sell
A 220 3
B 180 1
C 200 2
D 240 4
E 270 7
F 200 4
If sell > 3 then investment = sell * production_cost
If sell < 3 then investment = sell * 0.5 * production_cost(50% of production cost)
I have tried by the following way:
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
But the error is object sell not found
Then I have to compute
If investment >= 800 then produce = 1
If investment < 800 then produce = 0
Though I couldn't compute the variable investment I supposed it is as [by using calculator]
investment <- c(330,90,200,960,1890,800)
produce <- cut(investment,c(-Inf,800,Inf),labels=c("0","1"))
Here the problem is investment[6]=800. my attempt was to label it as 1. But it is labeling as 0.
Next i have to find the number of brands which are produce=1.
I tried this by the following way:
sum=0
for(i in 1:6){
if(produce[i]==1)sum=sum+1
} # end for loop
Is this right procedure and is there better way?
sellandproduction_costdo not exist butdata$sellanddata$production_costdo. post dput(data) so this is reproducible. look at?ifelsewithin(data, investment <- sell * production_cost * ifelse(sell>3, 1, 0.5))