作者goodparent1 (豆芽先生)
看板R_Language
標題[問題] txt檔如何直轉橫
時間Mon Oct 28 23:14:10 2013
[問題類型]:
程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
[軟體熟悉度]:
新手(沒寫過程式,R 是我的第一次)
[問題敘述]:
例如檔案
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 2010/12/31 7
。
。
Z 2010/12/31 -2
轉換成這種格式而且負數變成正數
A.price 。。 E.price。。Z.price
2000/01/31 1 5 1
2000/02/29 2 5 2
2000/03/31 3.3 6 3.5
。 。 。 。
。 。 。 。
2010/12/31 5 7 2
[程式範例]:
x<-read.table("D:/tmp/test.txt",sep=",")
x.wide=reshape(x,direction="wide",
+ v.names=c("price"),
+ timevar="name",
+ idvar="time")
錯誤在`[.data.frame`(data, , timevar) : undefined columns selected
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.44.214.90
※ 編輯: goodparent1 來自: 114.44.214.90 (10/28 23:23)
1F:→ celestialgod:我用你的例子 是成功的... 10/28 23:55
2F:→ Wush978:請問你的原始檔案的分隔符號是","嗎? 10/29 00:11
3F:→ goodparent1:原始檔分隔符號是空白。 10/29 01:35
4F:→ goodparent1:但是我sep用預設 會讀不進檔案。 10/29 01:44
5F:→ goodparent1:可能是因為我name,有些變數沒有名稱。 10/29 01:48
6F:→ goodparent1:可是na="NA",一樣讀不進檔案。 10/29 01:50
7F:→ celestialgod:你可以先用EXCEL讀進去 另存成csc 會比較好讀進去R 10/29 02:27
8F:→ celestialgod: v 10/29 02:27
9F:→ goodparent1:我的資料有三百多萬列,EXCEL放不下。 10/29 06:38
10F:→ ccwang002:我的經驗,如果是三百多萬列,一次轉 R 也不一定成功 10/29 10:04
11F:→ goodparent1:那請問我該如何把負數轉換成正數 10/29 22:50
12F:→ celestialgod:三百多萬列,R要算上數小時... 10/29 23:54
13F:→ celestialgod:正數只要用abs取正值就好 10/29 23:54
14F:→ celestialgod:你可以試試看用matlab轉... 10/29 23:55
15F:→ celestialgod:我把matlab code附於下篇文章 10/30 00:44
16F:→ celestialgod:另外,R也有unstack的function,你可以試試看!! 10/30 00:49
17F:→ celestialgod:unstack只能用在兩行的data frame,抱歉 10/30 13:33