How to rearrange some elements as a data frame

大憨熊 提交于 2019-12-13 04:22:15

问题


I want to rearrange some elements as a date frame. elements as follows:

  code<-000586
  t<-2013-12-28
  price<-10.23
  change<-2.56

I want use these elements form a data frame like this

 code      t       price    change 
 000586 2013-12-28 10.23     2.56
   > t1=2013-12-27
   > head(mydat)
     dm     sc  date      code
14 000586   sz 2013-12-27 000586.sz
15 000616   sz 2013-12-26 000616.sz
16 300295   sz 2013-12-25 300295.sz
17 000968   sz 2013-12-23 000968.sz
18 600023   sh 2013-12-19 600023.sh
20 600855   sh 2013-12-18 600855.sh

  >code<-mydat[1,1]
 > t1<-mydat[1,3]
  > class(wk)
    [1] "list"
  >wk
   $ErrorCode
   [1] 0

  $Data
  DATETIME    OPEN    CLOSE HIGH   PCT_CHG
 1 2013-12-27  12.2   12.5  127    0.25
 2 2013-12-30  12.0  12.4  12.6    0.26
 3 2013-12-31  11.3   11.2 11.5    2.1
 4 2014-01-02  11.1   11.4  11.5   0.2
 5 2014-01-03  11.4  11.5  11.6    0.32
 6 2014-01-06  10.2   10.4  10.6   0.34

 $Code
 [1] "000686"
 wk<-wk[2]$Data
 lo1<-wk[2,2])
 lo2<-wk[3,2]
 lo3<-wk[4,2]
 lc1<-wk[2,3]
 lc2<-wk[3,3]
 lc3<-wk[4,3]

I use the code m1<-c(code,t1,lo1=lo1,lo2=lo3,lo3) to form a data frame, but does not work; t1 changes to be not date value.


回答1:


dat <- data.frame(code="000586", t=as.Date("2013-12-28"), price=10.23, change=2.56)

will get you what you want, but if you are curious as to how to build such a data structure, be very wary of the analysis code you plan on cutting & pasting to arrive at an answer. This is really basic R.




回答2:


i have got the solutions. before

 m1<-c(code,t1,lo1=lo1,lo2=lo3,lo3)

i add the code

t1<-as.character(t1)

then it works.



来源:https://stackoverflow.com/questions/22572326/how-to-rearrange-some-elements-as-a-data-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!