作者shingetsu (shingetsu)
看板R_Language
標題[問題] 如何在取data其中一部份算mean
時間Fri Mar 11 23:17:49 2016
[問題類型]:
[軟體熟悉度]:
新手(沒寫過程式,R 是我的第一次)
[問題敘述]:
我想把data的資料分段算出平均數,例如 gsprice 第1筆到第50筆算出1個平均數,
第51筆到第100筆再取一個平均數
[程式範例]:
rm(list=ls()) #清除所有內容
library(TSA);library(tseries);library(FinTS);library(fGarch);library(dynlm)
datain <- read.table("C:\\R\\mydata.txt",header=TRUE)#讀入文字檔資料
attach(datain)
lsprice <- log(sprice)
lfprice <- log(fprice)
gsprice <- diff(lsprice)*100
gfprice <- diff(lfprice)*100
obsno <<- length(date)
datam <<-
matrix(cbind(date[-1],lsprice[-1],lfprice[-1],NFPI[-1],gsprice,gfprice),obsno-1,6)
colnames(datam) <- c("date","lsprice","lfprice","NFPI","gsprice","gfprice")
obs <<- length(datam[,1]) # obs is the sample size
y <<- matrix(0,obs,1)
y[,1]<- datam[,5]
[環境敘述]: win10 3.2.3
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 180.218.199.237
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1457709472.A.814.html
→ shingetsu: 不好意思,我有看過這篇,但是還是不太懂 03/12 00:17
→ celestialgod: library(zoo); ?rollapply看看吧 03/12 00:18
→ shingetsu: 請問 as.Date(31:35)是什麼意思 03/12 00:53
→ Wush978: split + sapply with mean? 03/12 11:22