how to create the data frame

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

how to create the data frame

luofeiyu
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]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to create the data frame

Ivan Calandra-2
Hi,

Let's say you original data.frame is called df1, you can do:
df2 <- data.frame(date=row.names(df1), weekly.returns=df1[[1]])

Next time, think about giving us you sample data through the dput()
function. It makes it way easier.

HTH,
Ivan

--
Ivan CALANDRA
Université de Bourgogne
UMR CNRS/uB 6282 Biogéosciences
6 Boulevard Gabriel
21000 Dijon, FRANCE
+33(0)3.80.39.63.06
[hidden email]
http://biogeosciences.u-bourgogne.fr/calandra

Le 11/07/12 07:35, 水静流深 a écrit :

> 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]]
>
>
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to create the data frame

sureshraj
In reply to this post by luofeiyu
Hi Dude!!

It seems that your column names are not sequentially arranged, I guess,

If so u just reorder them and do it your way...it will work..

for example,

colnames(data)[2]<–"weekly.returns" ## sometimes u can use like this as well, if u want to assign at last most column
                                                                   ## in your data frame
                                                                   ##  names(data)[ncol(data)]<–"weekly.returns"
and
colnames(data)[1]<– "date"

I hope it will work for your bug!!!
Reply | Threaded
Open this post in threaded view
|

Re: how to create the data frame

arun kirshna
In reply to this post by luofeiyu
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: 水静流深 <[hidden email]>
To: r-help <[hidden email]>
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]]


______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to create the data frame

arun kirshna
In reply to this post by sureshraj


Hi,

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)

str(dat1)
'data.frame':    5 obs. of  1 variable:
 $ weekly.returns: num  -0.01593 -0.01504 0.00535 -0.04932 -0.05267
#dates in the dataframe represents row names.
rownames(dat1)
[1] "2010-1-4"  "2010-1-11" "2010-1-18" "2010-1-25" "2010-2-1"

A.K.



----- Original Message -----
From: sureshraj <[hidden email]>
To: [hidden email]
Cc:
Sent: Wednesday, July 11, 2012 5:32 AM
Subject: Re: [R] how to create the data frame

Hi Dude!!

It seems that your column names are not sequentially arranged, I guess,

If so u just reorder them and do it your way...it will work..

for example,

colnames(data)[2]<–"weekly.returns" ## sometimes u can use like this as
well, if u want to assign at last most column
                                                                   ## in
your data frame
                                                                   ##
names(data)[ncol(data)]<–"weekly.returns"
and
colnames(data)[1]<– "date"

I hope it will work for your bug!!!

--
View this message in context: http://r.789695.n4.nabble.com/how-to-create-the-data-frame-tp4636111p4636117.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.