[R] Reading Data
Nutter, Benjamin
NutterB at ccf.org
Tue Oct 7 15:02:57 CEST 2008
Perhaps it would be easier to try something like this:
> df1 <- read.table(filename, ... ) # All columns read as characters
> df1 <- t(df1) # Transpose df1
> write.table <- (df1, newfile, ...) # Write the transposed data
# to a new file
> df2 <- read.table(newfile, header=TRUE, ...) # Read in transposed
data
I doubt it's the most computation-time-efficient, but conceptually it's
pretty quick.
Benjamin
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Rahul-A.Agarwal at ubs.com
Sent: Tuesday, October 07, 2008 8:28 AM
To: p.pagel at wzw.tum.de; r-help at r-project.org
Subject: [R] FW: Reading Data
Rahul Agarwal
Analyst
Equities Quantitative Research
UBS_ISC, Hyderabad
On Net: 19 533 6363
hi let me explain you the problem
we have a database which is in this format
Stocks 30-Jan-08 28-Feb-08 31-Mar-08 30-Apr-08
a 1.00 3.00 7.00 3.00
b 2.00 4.00 4.00 7.00
c 3.00 8.00 655.00 3.00
d 4.00 23.00 4.00 5.00
e 5.00 78.00 6.00 5.00
and we have a query which is in this format
Identifier weight Start_Date End_Date
a 6.76 31-Jan-06 31-Jan-07
e 2.86 28-Feb-06 28-Feb-07
f 22.94 31-Mar-06 30-Mar-07
y 30.05 28-Apr-06 30-Apr-07
h 20.55 31-May-06 31-May-07
d 6.76
f 2.86
r 22.94
okay now my task is to calculate returns for all the indentifiers for
a respective start and end date from table 1.
now length of start date and end date column is same and that of weight
and identifier is same.
i hope everything is clear now.
let me also send you the code that i have written but in my code i have
problem with the date format and also with the stocks name
data=read.table("H:/Rahul/london/david/rexcel/price.txt")
query=read.table("H:/Rahul/london/david/rexcel/prac.txt",header=TRUE)
data=as.matrix(data)
instrument=data[,1]
date=data[1,]
query=as.matrix(query)
q_ins=query[,1]
wt=query[,2]
q_sd=query[,3]
q_ed=query[,4]
returns=function(I,SD,ED){
p=rep(0,2)
for(i in 2:length(instrument))
{
if(instrument[i]==I)
{
for(j in 2:length(date))
{
if(date[j]==SD)
p[1]=data[i,j]
}
for(j in 2:length(date))
{
if(date[j]==ED)
p[2]=data[i,j]
}
}
returns=(p[2]/p[1])-1
}
#print(p)
#print(I)
return(returns)
}
##The original Funda##
matrix_ret=matrix(0,length(q_ins),length(q_sd))
for(i in 1:length(q_sd))
{
for(j in 1:length(q_ins))
{
matrix_ret[j,i]=returns(q_ins[j],q_sd[i],q_ed[i])
}
}
#Removing NA from the matrix
matrix_ret1=sapply(X=matrix_ret, FUN=function(x)
ifelse(is.na(x),0.00,x))
matrix_ret=matrix(matrix_ret1,length(q_ins),length(q_sd))
wt_ret=matrix(0,length(q_sd),1)
for(i in 1:length(q_sd))
{
for(j in 1:length(q_ins))
{
wt_ret[i]=wt_ret[i]+(wt[j]*matrix_ret[j,i])
}
}
result=cbind(q_ed,wt_ret)
Rahul Agarwal
Analyst
Equities Quantitative Research
UBS_ISC, Hyderabad
On Net: 19 533 6363
-----Original Message-----
From: Gustaf Rydevik [mailto:gustaf.rydevik at gmail.com
<mailto:gustaf.rydevik at gmail.com> ]
Sent: Tuesday, October 07, 2008 2:19 PM
To: Agarwal, Rahul-A
Cc: R-SIG-robust at r-project.org; r-help at r-project.org
Subject: Re: [R] Reading Data
On Tue, Oct 7, 2008 at 10:36 AM, <Rahul-A.Agarwal at ubs.com> wrote:
>
> Hi,
> I have a data in which the first row is in date format and the first
> column is in text format and rest all the entries are numeric.
> Whenever I am trying to read the data using read.table, the whole of
> my data is converted in to the text format.
>
> Please suggest what shall I do because using the numeric data which
> are prices I need to calculate the return but if these prices are not
> numeric then calculating return will be a problem
>
> regards
>
> Rahul Agarwal
> Analyst
> Equities Quantitative Research
> UBS_ISC, Hyderabad
> On Net: 19 533 6363
>
Hi,
A single column in a data frame can't contain mixed formats.
In the absence of example data, would guess one of the following could
work :
1)
read.table("data.txt",skip=1, header=T) ## If you have headers
2)
read.table("data.txt", header=T) ## If the date row is supposed to be
variable names.
3)
read.table("data.txt",skip=1) ## If there are no headers, and you want
to ignore the date
regards,
Gustaf
--
Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik
[[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.
P Please consider the environment before printing this e-mail
Cleveland Clinic is ranked one of the top hospitals
in America by U.S. News & World Report (2008).
Visit us online at http://www.clevelandclinic.org for
a complete listing of our services, staff and
locations.
Confidentiality Note: This message is intended for use\...{{dropped:13}}
More information about the R-help
mailing list