0

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?

3
  • sell and production_cost do not exist but data$sell and data$production_cost do. post dput(data) so this is reproducible. look at ?ifelse Commented Jun 28, 2013 at 16:12
  • 1
    Tip: within(data, investment <- sell * production_cost * ifelse(sell>3, 1, 0.5)) Commented Jun 28, 2013 at 16:14
  • 1
    Given this and your previous question, you may want to start by reading cran.r-project.org/doc/manuals/R-intro.pdf, and start with Section 6.3 regarding dataframes. Commented Jun 28, 2013 at 16:16

2 Answers 2

1

Use within, it creates an environment and returns a new dataframe:

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

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

NOTE: this code sets

 investment = sell * production_cost * 0.5

if sell == 3

Hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

why doesn't here with work? and sometimes the expr argument of within works without braces and sometimes doesn't. Why?
see ?with. Basically with returns the evaluated expression while within returns the modified dataframe.
0

Assume your dataframe is sample. Following code is not tested

#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))

2 Comments

plyr is too much overhead for this simple task. Use plyr to do complex stuff with your data.
@Fernando : I will leave that to OP to decide.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.