0

Possible Duplicate:
create new data frame from a function of other data frames

I got a little help with my very first question of SOF and I don't know how to respond to the respondent. So, I'm posting again with sample code (Should have done this the first time - I'm learning).

I have two data frames. Let’s pretend for the sake of explanation:

df1 Columns represents types of gains: corn, oats, wheat, etc. Rows represents the month of the year, jan, feb, etc Elements represents the price per ton for that type of grain purchased during that particular month.

df2 Columns representing countries: Spain, Chile, Mexico, etc. The rows of this frame represent additional costs, maybe: Packaging cost, Shipping cost, Country import tax, Inspection fees, etc. for each country.

Now I want to build a third data frame:

df3 It is to represent the total cost of a combination of grains (for example 10% corn, 50% oats, ...) with the associated costs for shipping, tax, etc. for the countries, for each month. Assume there is an equation (using data from df1 and df2) to compute the total cost per country per month for a given combination of grains and the additional costs for each country.

Anotherwords, df3 has 12 rows (months) and as many columns as there are countries. It's elements are the total cost of grain + costs for each country, for each month.

Two minutes in Excel/Gnumeric, 15 minutes in Fortran or C, two day struggling with R Cookbook and internet searches. And, I have no one down the hall to yell, "Hey, Kevin, how do you do this in R...?"

So simple, but for a newbie, I’m overlooking some fundamental point..

Thanks in advance and Here's my pretend code that illustrates my problem.

Ed

# build df1 - cost of grains (with goofy data so I can track the arithemetic)
  v1 <- c(1:12)
  v2 <- c(13:24)
  v3 <- c(25:36)
  v4 <- c(37:48)
  grain <- data.frame("wheat"=v1,"oats"=v2,"corn"=v3,"rye"=v4)

  grain


# build df2 - additional costs (again, with goofy data to see what is being used where and when)
  w1 <- c(1.3:4.3)
  w2 <- c(5.3:8.3)
  w3 <- c(9.3:12.3)
  w4 <- c(13.3:16.3)
  cost <- data.frame("Spain"=w1,"Peru"=w2,"Mexico"=w3,"Kenya"=w4)
  row.names(cost) <- c("packing","shipping","tax","inspection")

  cost


# assume 10% wheat, 30% oats and 60% rye with some clown-equation for total cost

# now for my feeble attemp at getting a dataframe that has 12 rows (months) and 4 column (countries)

  total_cost <- data.frame( 0.1*grain[,"wheat"] +
                            0.3*grain[,"oats"] +
                            0.6*grain[,"rye"] +
                            cost["packing","Mexico"] +
                            cost["shipping","Mexico"] +
                            cost["tax","Mexico"]  +
                            cost["inspection","Mexico"] )
  total_cost

# this gives the correct values for the total cost for Mexico, for each month.

# and if I plug in the other countries, I get correct answers for that country
# I guess I can run a loop over the counties, but this is R, not Fortran or C. 

# btw, my real equation is considerably more complicated, using functions involving
# multiple columns of df1 and df2 data, so there is no "every column of a df1 get 
#multipied by... or any one-to-one column-row matches.
Community
  • 1
  • 1
  • 2
    Welcome to SO! Generally, reposting questions is frowned upon (so this version will likely be closed as a duplicate). Instead, edit your original question and add the necessary code + details. – joran Sep 10 '12 at 19:15
  • For more info on how to ask questions, including how to edit them, see the [FAQ](http://stackoverflow.com/faq#howtoask) – BenBarnes Sep 10 '12 at 19:50

0 Answers0