[R] Total and heading of portfoilo table

Petr PIKAL petr.pikal at precheza.cz
Tue Feb 16 16:44:49 CET 2010


Hi

r-help-bounces at r-project.org napsal dne 16.02.2010 08:05:08:

> 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,])
>  
> }

If I am not mistaken and if you ensure that column positions in both data 
frames are the same.

 rowSums(mapply("*",  Y, X))

Shall do the trick

Regards
Petr



>  
> 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_sanchez09 at yahoo.com> wrote:
> 
> 
> From: Sarah Sanchez <sarah_sanchez09 at yahoo.com>
> Subject: [R] Total and heading of portfoilo table
> To: r-help at 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 at 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.
> 
> 
> 
> 
> tp://downloads.yahoo.com/in/internetexplorer/
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list