看板 R_Language 關於我們 聯絡資訊
我的測試檔案 ----------------tmp.txt------------------ name,time,price A,2000/01/31,-1 A,2000/02/29,2 A,2000/03/31,-3.3 A,2010/12/31,5 E,2000/01/31,-4 E,2000/02/29,5 E,2000/03/31,-1.1 E,2010/12/31,5 Z,2000/01/31,-10 Z,2000/02/29,7 Z,2000/03/31,-1.8 Z,2010/12/31,-2 ---------------end of file---------------- R code: x=read.table("tmp.txt",sep=",", header=TRUE) x[,3] = abs(x[,3]) x.wide=reshape(x,direction="wide", v.names=c("price"), timevar="name", idvar="time") output: time price.A price.E price.Z 1 2000/01/31 -1.0 -4.0 -10.0 2 2000/02/29 2.0 5.0 7.0 3 2000/03/31 -3.3 -1.1 -1.8 4 2010/12/31 5.0 5.0 -2.0 matlab code ---------------new.txt------------- name time price A 2000/01/31 -1 A 2000/02/29 2 A 2000/03/31 -3.3 A 2010/12/31 5 E 2000/01/31 -4 E 2000/02/29 5 E 2000/03/31 -1.1 E 2010/12/31 5 Z 2000/01/31 -10 Z 2000/02/29 7 Z 2000/03/31 -1.8 Z 2010/12/31 -2 ------------------------------------ new.txt之間是tab,是空白的話,下面'\t'請改成' ' Dat = readtable('new.txt', 'Format', '%s%s%f', 'delimiter', '\t'); Dat.price = abs(Dat.price); W = unstack(Dat, 'price', 'name'); Matlab的速度很快,可以試試看。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 36.238.81.92
goodparent1:有辦法用r跑嗎,MATLAB 檔案很大 很難下載。 11/01 17:44
R的code就在上面,只是用sep=",",如果你的間隔是空白就改成sep=" " ※ 編輯: celestialgod 來自: 218.164.79.5 (11/01 19:09)
goodparent1:我是指負數轉正數的code 如果我要用r跑 該怎麼寫 11/02 11:45
goodparent1:不好意思,我看到了。 11/02 11:47
goodparent1:可是我用ABS跑,只能跑到1800多列,剩下的檔案會遺失 11/03 22:41
celestialgod:我沒那麼大的檔案 我沒法測試= = 11/03 23:17
我改成這樣,結果如下: x=read.table("test.txt", header=TRUE) x[,3] = abs(as.numeric(x[,3])) x.wide=reshape(x,direction="wide", v.names=c("Adjprc"), timevar="name", idvar="time") > dim(x.wide) [1] 504 1986 ※ 編輯: celestialgod 來自: 36.239.250.66 (11/04 19:03)
goodparent1:可是這樣轉出來,我的時間列就不見了.... 11/05 20:22
Wush978:push 11/07 22:17
Wush978:my code: 11/07 22:18
Wush978:x=read.table("test.txt", header=TRUE) 11/07 22:18
Wush978:x$time=as.Date(as.character(x$time), format="%Y%m%d") 11/07 22:18
Wush978:x$Adjprc=abs(x$Adjprc) 11/07 22:19
Wush978:reshape部分相同, 最後得到如: `head(x.wide[,1:5])` 11/07 22:19
Wush978:第一行就是時間(日期) 11/07 22:19