s-news
[Top] [All Lists]

Re: query on column vector manipulation

To: 2116242101@jcom.home.ne.jp, s-news@lists.biostat.wustl.edu
Subject: Re: query on column vector manipulation
From: Bill.Venables@CMIS.CSIRO.AU
Date: Thu, 22 Aug 2002 14:41:00 +1000
Moto Hasegawa asks:
-----Original Message-----
From: motohasegawa [mailto:2116242101@jcom.home.ne.jp]
Sent: Thursday, August 22, 2002 8:04 AM
To: splus
Subject: [S] query on column vector manipulation

Dear S-PLUS users:
 
I want to make a script that calculates returns among variables in a column vector. Specifically, I need a script that calculates, ln(x[,i+1]/x[,i]) for all elements in a column vector x. Here, i denotes ith row.
[WNV] In S the first index denotes the row, the second denotes the column.
 
 For this purpose, I wrote the following "script" but it does not work. It will be of great help if you could give me some advice, suggestion, correction.

calcLN <- function(x) {
 
 x1<-c(matrix(unlist(x),nrow=nrow(x)))
 x1<-log(numeric(x1))
 for(i in 1:nrow(x1)-1) {
  x1rtn[i]<-x1[i+1]-x1[i]
  i<-i+1
 }
 x1rtn
 
}
[WNV] the problem is with the line x1 <- log(numeric(x1)).  You probably meant
 
    x1 <- log(as.numeric(x1))
 
but your function is way too complicated.  The usual way to do it would be
 
    calcLOG <- function(x) diff(log(x))
 
finish.  This makes you wonder if you need a special function to do the job at all...
 
Bill Venables. 
 
 
Regards,
 
Moto
<Prev in Thread] Current Thread [Next in Thread>