4
我有一個小問題,as.Date
函數。我把一個prn數據加載爲csv文件,我的老師說沒關係,然後當我嘗試以標準方式(y,m,d)設置我的日期時,它給了我一個瘋狂的數字。 PZU文件是波蘭公司的股票價格。另外,我也不確定如何設定時間。我真的很感謝幫助。僅用於一般信息,我正在使用R軟件和R studio界面。as.Date函數不會轉換日期
pzu<-read.csv("PZU.prn",header=F)[,1:7]
names(pzu)<-c("name","date","time","open","high","low","close")
head(pzu)
# name date time open high low close
#1 PZU 20100512 845 349 349 349 349
#2 PZU 20100512 845 349 349 349 349
#3 PZU 20100512 845 349 349 349 349
#4 PZU 20100512 845 349 349 349 349
#5 PZU 20100512 845 349 349 349 349
#6 PZU 20100512 845 349 349 349 349
class(pzu$date) # output is an "integer"
str(pzu)
#data.frame': 960638 obs. of 7 variables:
# $ name : Factor w/ 1 level "PZU": 1 1 1 1 1 1 1 1 1 1 ...
# $ date : int 20100512 20100512 20100512 20100512 20100512 20100512 20100512 20100512 20100512 20100512 ...
# $ time : int 845 845 845 845 845 845 845 845 845 845 ...
# $ open : num 349 349 349 349 349 349 349 349 349 349 ...
# $ high : num 349 349 349 349 349 349 349 349 349 349 ...
# $ low : num 349 349 349 349 349 349 349 349 349 349 ...
# $ close: num 349 349 349 349 349 349 349 349 349 349 ...
pzu$date<-as.Date(pzu$date)
head(pzu)
#name date time open high low close
#1 PZU 7003-05-03 845 349 349 349 349
#2 PZU 7003-05-03 845 349 349 349 349
#3 PZU 7003-05-03 845 349 349 349 349
#4 PZU 7003-05-03 845 349 349 349 349
#5 PZU 7003-05-03 845 349 349 349 349
#6 PZU 7003-05-03 845 349 349 349 349
是的,它的工作,非常感謝。我很感激。另外,請你告訴我如何格式化時間,時間的類是「整數」。我希望它看起來像站立時間格式,例如:8:45非常感謝你。 – Serdar
@ user3157869:我編輯了答案也轉換時間,希望它有幫助。 –
再次感謝您,我感謝您的幫助。 – Serdar