[R] how to create the data frame

arun smartpink111 at yahoo.com
Wed Jul 11 14:21:28 CEST 2012


Hi,

Try this:
dat1<-read.table(text="
    weekly.returns
2010-1-4        -0.015933327
2010-1-11        -0.015042868
2010-1-18        0.005350297
2010-1-25        -0.049324703
2010-2-1        -0.052674121
",sep="",header=TRUE)
dat2<-data.frame(date=row.names(dat1),weekly.returns=dat1[1])
row.names(dat2)<-1:nrow(dat2)
 class(dat2$date)
#[1] "factor"

#if date column needed to be formatted to date class
dat2$date<-as.Date(dat2$date, format="%Y-%m-%d")
 dat2
        date weekly.returns
1 2010-01-04   -0.015933327
2 2010-01-11   -0.015042868
3 2010-01-18    0.005350297
4 2010-01-25   -0.049324703
5 2010-02-01   -0.052674121

 class(dat2$date)
#[1] "Date"



A.K.




----- Original Message -----
From: 水静流深 <1248283536 at qq.com>
To: r-help <r-help at r-project.org>
Cc: 
Sent: Wednesday, July 11, 2012 1:35 AM
Subject: [R] how to create the data frame

there is a data frame £¬x
            weekly.returns
2010-1-4        -0.015933327
2010-1-11        -0.015042868
2010-1-18        0.005350297
2010-1-25        -0.049324703
2010-2-1        -0.052674121
when i input 
colnames(data)[1] <- 'date'
it becomes
                      date
2010-1-4        -0.015933327
2010-1-11        -0.015042868
2010-1-18        0.005350297
2010-1-25        -0.049324703
2010-2-1        -0.052674121
how can i change ti into:
   date        weekly.returns
1  2010-1-4        -0.015933327
2  2010-1-11        -0.015042868
3  2010-1-18        0.005350297
4  2010-1-25        -0.049324703
5  2010-2-1        -0.052674121
    [[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