say you read the quantity.csv file into a variable called 'quantity'... similarly, 'equity_price.csv' to equity.
sweep(equity, 2, quantity, "*") b On Tue, Feb 16, 2010 at 11:20 AM, Sarah Sanchez <sarah_sanche...@yahoo.com> wrote: > Dear Madam / R helpers, > > Unfortunately the solution you have suggested is not working in the sense > that the quantities are not multplying the rows but its multiplying > columnwise and hence I am getting all wrong results. > > I am again submitting my problem. Please guide me. > > I have two input files as 'quantity.csv' and 'equity_price.csv' as (for > example) given below. > > 'quantity.csv' > GOOG YHOO > 1000 100 > > 'equity_price.csv' > sr_no GOOG_price YHOO_price > 1 15.22 536.40 > 2 15.07 532.97 > 3 15.19 534.05 > 4 15.16 531.86 > 5 15.11 532.11 > > > My problem is to calculate the portfolio value for each of these 5 days > (actually my portfolio consists of 47 comanies and prices taken are for last > 1 year). > > My requirement is to find out the portfolio value for all the five days. In > other words, I need to multiply respective equity prices for all the five > days with the corresponding quatities to have following output. > > GOOG YHOO > > 15220 53640 > 15070 53297 > 15190 53405 > 15160 53186 > 15110 53211 > > Please guide > > Regards > > Sarah > > > > > --- On Tue, 2/16/10, Madhavi Bhave <madhavi_bh...@yahoo.com> wrote: > > > From: Madhavi Bhave <madhavi_bh...@yahoo.com> > Subject: Re: [R] Total and heading of portfoilo table > To: r-help@r-project.org, "Sarah Sanchez" <sarah_sanche...@yahoo.com> > Date: Tuesday, February 16, 2010, 7:05 AM > > > > > > > > Hi! > > I am not expert in R, but perhaps you can try the following - > > X = as.numeric(read.csv('quantity.csv')) > Y = read.csv('equity_price.csv') > Y = Y[, -1] > > Z = X*Y > > port_val = NULL > > for(i in 1 : nrow(Z)) > { > > port_val[i] = sum(Z[i,]) > > } > > write.csv(data.frame(Z, port_val = port_val), 'PORTFOLIO.csv', row.names = > FALSE) > > > I am sure the experts will have much simpler way to address this problem. > > Regards > > Madhavi > > --- On Mon, 15/2/10, Sarah Sanchez <sarah_sanche...@yahoo.com> wrote: > > > From: Sarah Sanchez <sarah_sanche...@yahoo.com> > Subject: [R] Total and heading of portfoilo table > To: r-help@r-project.org > Date: Monday, 15 February, 2010, 10:08 PM > > > Dear R helpers, > > I have two input files as 'quantity.csv' and 'equity_price.csv' as (for > example) given below. > > 'quantity.csv' > GOOG YHOO > 1000 100 > > > 'equity_price.csv' > sr_no GOOG_price YHOO_price > 1 15.22 536.40 > 2 15.07 532.97 > 3 15.19 534.05 > 4 15.16 531.86 > 5 15.11 532.11 > > My problem is to calculate the portfolio value for each of these 5 days > (actually my portfolio > consists of 47 comanies and prices taken are for last 1 year). > > I had defined > > X = read.csv('quantity.csv') > Y = read.csv('equity_price.csv') > > I have tried the loop > > Z = array() > > for (i in 1:2) > { > Z[i] = (X[[i]]*Y[i]) > } > > # When I write this dataframe as > > write.csv(data.frame(Z), 'Z.csv', row.names = FALSE) > > When I open 'Z.csv' file, I get > > c.2500L..3300L..4500L..1000L..4400L. > c.14000L..45000L..48000L..26000L..15000L. > 2500 14000 > 3300 45000 > 4500 48000 > 1000 26000 > 4400 15000 > > My requirement is to have the column heads and the portfolio total as > GOOG YHOO Total > 2500 14000 16500 > 3300 45000 48300 > 4500 48000 52500 > 1000 26000 27000 > 4400 15000 19400 > > > Please guide > > Regards > > Sarah > > > > > > [[alternative HTML version deleted]] > > > -----Inline Attachment Follows----- > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > > > [[elided Yahoo spam]] > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.