很抱歉打扰你,我不确定这已经回答了问题或教程级问题。但我找不到这样的东西(rollapply,R Loop)或我无法理解答案,因为我是初学者。
我有这样的Dataframe:
> temptable
Date Volume Open High Low Close
1 20160502 140552 1247000 1262000 1245000 1250000
2 20160503 158066 1267000 1270000 1256000 1261000
3 20160504 294055 1272000 1290000 1262000 1290000
4 20160509 312894 1290000 1300000 1285000 1299000
5 20160510 171191 1299000 1300000 1288000 1296000
6 20160511 176688 1296000 1299000 1287000 1292000
7 20160512 152302 1292000 1292000 1275000 1281000
8 20160513 245698 1281000 1281000 1251000 1253000
9 20160516 234403 1253000 1263000 1247000 1248000
10 20160517 183671 1249000 1265000 1249000 1264000
11 20160518 183392 1264000 1271000 1255000 1268000
12 20160519 200249 1268000 1277000 1266000 1270000
13 20160520 159211 1270000 1280000 1269000 1269000
14 20160523 162251 1269000 1286000 1269000 1286000
15 20160524 208692 1286000 1289000 1268000 1271000
16 20160525 237255 1283000 1298000 1274000 1295000
17 20160526 253073 1299000 1303000 1295000 1296000
18 20160527 274891 1300000 1301000 1273000 1282000
我想创建列来计算rrr(包TTR'函数)。我想做两个案子。我该怎么做?谢谢。
library(zoo)
library(stocks)
library(RSQLite)
filename = "C:/Users/Kun/Documents/Dashin/testcon2.db"
sqlite.driver = dbDriver("SQLite")
db = dbConnect(sqlite.driver,dbname = filename)
database = dbListTables(db)
temptable = dbReadTable(db,'A005930')
temptable
pr = temptable$Close
case 1: 1~N
rrr(prices=pr[1:10])
rrr(prices=pr[1:11])
rrr(prices=pr[1:12])
rrr(prices=pr[1:13])
.
.
case 2: (N+1:N+1) last 10
rrr(prices=pr[1:10])
rrr(prices=pr[2:11])
rrr(prices=pr[3:12])
我英语不太好。所以我不知道自己该做什么。
答案 0 :(得分:2)
首先,包rrr
中没有功能TTR
,它位于包stocks
中。
对于您的问题,发布数据集示例的最佳方法是使用?dput
。
#dput(temptable)
temptable <-
structure(list(Date = c(20160502L, 20160503L, 20160504L, 20160509L,
20160510L, 20160511L, 20160512L, 20160513L, 20160516L, 20160517L,
20160518L, 20160519L, 20160520L, 20160523L, 20160524L, 20160525L,
20160526L, 20160527L), Volume = c(140552L, 158066L, 294055L,
312894L, 171191L, 176688L, 152302L, 245698L, 234403L, 183671L,
183392L, 200249L, 159211L, 162251L, 208692L, 237255L, 253073L,
274891L), Open = c(1247000L, 1267000L, 1272000L, 1290000L, 1299000L,
1296000L, 1292000L, 1281000L, 1253000L, 1249000L, 1264000L, 1268000L,
1270000L, 1269000L, 1286000L, 1283000L, 1299000L, 1300000L),
High = c(1262000L, 1270000L, 1290000L, 1300000L, 1300000L,
1299000L, 1292000L, 1281000L, 1263000L, 1265000L, 1271000L,
1277000L, 1280000L, 1286000L, 1289000L, 1298000L, 1303000L,
1301000L), Low = c(1245000L, 1256000L, 1262000L, 1285000L,
1288000L, 1287000L, 1275000L, 1251000L, 1247000L, 1249000L,
1255000L, 1266000L, 1269000L, 1269000L, 1268000L, 1274000L,
1295000L, 1273000L), Close = c(1250000L, 1261000L, 1290000L,
1299000L, 1296000L, 1292000L, 1281000L, 1253000L, 1248000L,
1264000L, 1268000L, 1270000L, 1269000L, 1286000L, 1271000L,
1295000L, 1296000L, 1282000L)), .Names = c("Date", "Volume",
"Open", "High", "Low", "Close"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18"))
然后,对于运行的答案,我只需要包stocks
,而不是其他包。
library(stocks)
pr <- temptable$Close
sapply(seq_along(pr)[-(1:9)], function(i) rrr(pr[1:i]))
sapply(seq_along(pr)[-((length(pr) - 9):length(pr))], function(i) rrr(pr[i:(i + 9)]))