我正在用R创建一个二十一点模拟器.下面的代码成功创建了我想要的卡片组.(对于那些参加比赛的人,我将在稍后处理一个Ace的价值).
我的问题是,有没有更好的方法来创建不涉及while循环加上双循环的套牌?我对double for循环有更多的问题.while循环可能是不可避免的,因为创建的套牌数量是可变的.
我也初始化一个空数据框,我知道这不是最佳实践,但是,在这种情况下,数据集非常小,不会影响性能.
最后,R中是否有相当于i ++的东西?我也一直在使用java编程并且已经习惯了它.
谢谢.
createDeck <- function(totalNumOfDecks = 2)
{
suits <- c("Diamonds", "Clubs", "Hearts", "Spades")
cards <- c("Ace", "Deuce", "Three", "Four","Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King")
values <- c(0,2,3,4,5,
6,7,8,9,10,
10,10,10)
deck <- data.frame(Suit=character(0), Card=character(0), Value=numeric(0))
numOfDecks = 1
while (numOfDecks <= totalNumOfDecks){
for (i in suits){
for (j in cards){
deck <- rbind.data.frame(deck, cbind.data.frame(j, i, values[match(j, cards)]))
}
}
numOfDecks = numOfDecks + 1
}
print(deck)
}
Run Code Online (Sandbox Code Playgroud) 我正在使用R中的ibrokers软件包,我正在尝试为交易设置多个收盘价.例如,以106美元的价格购买100股AAPL,以107美元的价格卖出50股,以108美元的价格卖出50股,止损价为105美元.
当我发送多个获利订单时,似乎忽略了50的数量,而是我得到两个卖单,每个100股.
这是我正在运行的代码
tws <- twsConnect()
stock <- twsEquity("AAPL")
parentLongId <- reqIds(tws)
parentLongOrder <- twsOrder(parentLongId, action="BUY", totalQuantity = 100,
orderType = "LMT", lmtPrice = 106,
transmit=TRUE)
placeOrder(tws, stock, parentLongOrder)
childLongProfitId <- reqIds(tws)
childLongProfitOrder <- twsOrder(childLongProfitId, action="SELL", totalQuantity = 50,
orderType = "LMT", lmtPrice = 107,
transmit=TRUE, parentId = parentLongId)
placeOrder(tws, stock, childLongProfitOrder)
childLongProfitId2 <- reqIds(tws)
childLongProfitOrder2 <- twsOrder(childLongProfitId2, action="SELL", totalQuantity = 50,
orderType = "LMT", lmtPrice = 108,
transmit=TRUE, parentId = parentLongId)
placeOrder(tws, stock, childLongProfitOrder2)
childLongStopId <- reqIds(tws)
childLongStopOrder <- …Run Code Online (Sandbox Code Playgroud) 我试图在需要登录的网站上抓一页,并且一直在收到403错误.
我已经为我的网站修改了这2个帖子的代码,使用rvest或httr登录网页上的非标准表单以及如何重用会话以避免在使用rvest进行重复登录时重复登录?
library(rvest)
pgsession <- html_session("https://www.optionslam.com/earnings/stocks/MSFT?page=-1")
pgform <- html_form(pgsession)[[1]]
filled_form <- set_values(pgform, 'username'='user', 'password'='pass')
s <- submit_form(pgsession, filled_form) # s is your logged in session
Run Code Online (Sandbox Code Playgroud)
代码运行时,我收到以下消息:
Submitting with 'NULL'
Warning message:
In request_POST(session, url = url, body = request$values, encode = request$encode, :
Forbidden (HTTP 403).
Run Code Online (Sandbox Code Playgroud)
我也通过这种方式运行代码,通过在注释中将user_agent更新为RS建议,但是,我收到与上面相同的错误.
library(rvest)
library(httr)
uastring <- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36"
pgsession <- html_session("https://www.optionslam.com/earnings/stocks/MSFT?page=-1", user_agent(uastring))
pgform <- html_form(pgsession)[[1]]
filled_form <- set_values(pgform, 'username'='user', 'password'='pass')
s …Run Code Online (Sandbox Code Playgroud) 我正在 R 中绘制直方图,前两个频率被组合到一个我不想要的容器中。数据中有 7 个可能的值,我想要 7 个容器,而不是 6 个。

R绘制的直方图的信息是,
$breaks
[1] 9 10 11 12 13 14 15
$counts
[1] 27 6 5 4 1 11
$density
[1] 0.50000000 0.11111111 0.09259259 0.07407407 0.01851852 0.20370370
$mids
[1] 9.5 10.5 11.5 12.5 13.5 14.5
$xname
[1] "data$hour"
$equidist
[1] TRUE
attr(,"class")
[1] "histogram"
Run Code Online (Sandbox Code Playgroud)
问题在于 $counts,它们应该是:
9-20
10-7
11-6
12-5
13-4
14-1
15-11
Run Code Online (Sandbox Code Playgroud)
直方图和信息生成的数据和命令是:
temp <- c(9, 9, 9, 11, 12, 14, 15, 9, 9, 9, 10, 9, 13, 13, 15, 15, …Run Code Online (Sandbox Code Playgroud) 我正在使用chartSeries,而Y轴正在被切断.我希望右边的价格扩展到2位小数.这似乎是边距或字体大小的问题,但在做了一些搜索之后,我找不到任何地方来调整这些选项.我说利润率因为图表左侧似乎有足够的空间.
有任何想法吗?谢谢.

以下是上图的可重现代码:
require (zoo)
require(quantmod)
data <- structure(list(Date = structure(list(sec = c(0, 0, 0, 0, 0, 0
), min = 0:5, hour = c(15L, 15L, 15L, 15L, 15L, 15L), mday = c(3L,
3L, 3L, 3L, 3L, 3L), mon = c(0L, 0L, 0L, 0L, 0L, 0L), year = c(114L,
114L, 114L, 114L, 114L, 114L), wday = c(5L, 5L, 5L, 5L, 5L, 5L
), yday = c(2L, 2L, 2L, 2L, 2L, 2L), isdst = c(0L, 0L, 0L, 0L,
0L, 0L)), .Names …Run Code Online (Sandbox Code Playgroud) 我正在从这个页面抓取 R 中的数据,http://finviz.com/screener.ashx? v= 111&f=earningsdate_nextdays5显示弹出广告。这些广告会干扰脚本,所以我想启用广告拦截器扩展:https ://chrome.google.com/webstore/detail/adblock/gighmmpiobklfepjocnamgkkbiglidom
我在这里使用 RSelenium 包文档中的代码,https: //cran.r-project.org/web/packages/RSelenium/RSelenium.pdf
我通过打开 chrome 浏览器并导航到:chrome://version/ 找到了配置文件目录。这是我常用的配置文件,它启用了广告拦截器扩展。
但是,当我打开 chrome 时,没有广告拦截器。我查看了这个页面,http: //scottcsims.com/wordpress/?p=450,他建议使用似乎没有在 RSelenium 中实现的 add_extension 方法。
关于如何在 R 打开的浏览器中启用广告拦截器的任何想法?
我的代码到目前为止。请注意,这是在 mac 上完成的,当然您的用户名将与我的不同,因此请务必将 getChromeProfile 中的第一个参数更改为您在此页面上的 Profile Path 中找到的内容,chrome://version/
require(RSelenium)
RSelenium::startServer()
cprof <- getChromeProfile("/Users/<username>/Library/Application Support/Google/Chrome/", "Profile 1")
remDr <<- remoteDriver(browserName = "chrome", extraCapabilities = cprof)
remDr$open()
appURL <- "http://finviz.com/screener.ashx?v=111&f=earningsdate_nextdays5"
remDr$navigate(appURL)
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写二进制搜索函数来查找fun区间中函数的根[,]:
这是我所拥有的,但却遗漏了标记:
def binarySearchIter(fun, start, end, eps=1e-10):
'''
fun: funtion to fund the root
start, end: the starting and ending of the interval
eps: the machine-precision, should be a very small number like 1e-10
return the root of fun between the interval [start, end]
'''
root = (start + end)/2
print(root)
answer=fun(root)
if abs(answer) <= eps:
print(root)
return root
elif answer - eps > 0:
binarySearchIter(fun, start, root, eps=1e-10)
else:
binarySearchIter(fun, root, end, eps=1e-10)
Run Code Online (Sandbox Code Playgroud)
这是我用来测试的功能:
def …Run Code Online (Sandbox Code Playgroud) 我正在学习如何在R中使用nls函数,并且遇到了一些问题.我现在只是试图重新创建研究论文中的曲线.该模型符合1987年股市崩盘前股市走势的曲线.
我已经定义了一个函数func,如下所示:
func <- function(a,b,tc,t){
a+b*log(tc-t)
}
Run Code Online (Sandbox Code Playgroud)
我这样打电话给nls:
nls1 <- nls(Y ~ func(a,b,tc,t), data2, start=list(a=0, b=1, tc=1466, t=1))
Run Code Online (Sandbox Code Playgroud)
data2是一个由两列组成的数据框,一列是日期,另一列是值.有1466行.
head(data2)
Date Y
1 1/4/82 882.52
2 1/5/82 865.30
3 1/6/82 861.02
4 1/7/82 861.78
5 1/8/82 866.53
6 1/11/82 850.46
Run Code Online (Sandbox Code Playgroud)
我运行nls时收到以下消息,
Error in qr(.swts * attr(rhs, "gradient")) :
dims [product 4] do not match the length of object [1466]
In addition: Warning message:
In .swts * attr(rhs, "gradient") :
longer object length is not a multiple of shorter object length …Run Code Online (Sandbox Code Playgroud) 我正在努力做一些应该相当简单的事情,但是,我已经阅读了至少15种方法,并且似乎无法让它发挥作用.
这是一个示例数据集:
9:30:01 584.7
9:30:01 590
9:30:01 595
9:30:02 584.51
9:30:03 584.62
9:30:04 584.44
9:30:05 584.05
Run Code Online (Sandbox Code Playgroud)
我只想要每秒一行,所以前三行中只需要一行.我不在乎它是第一个还是最后一个,但我一直使用的代码保留了最后一个,在这种情况下为595.
我这样做的方法是使用for循环清除与其下面的行具有相同时间的行的内容.然后我对整个范围进行排序.
我想有一种简单的方法可以简单地从get go中删除额外的行.但是,当我在范围上使用delete而不是clear时,它不会删除所有重复的行.
这是我想要的数据:
9:30:01 595
9:30:02 584.51
9:30:03 584.62
9:30:04 584.44
9:30:05 584.05
Run Code Online (Sandbox Code Playgroud)
我需要在整张纸上发生这种情况.时间是B列,值是C列.
这是我正在使用的代码,
LastRow = ActiveSheet.UsedRange.row - 1 + _
ActiveSheet.UsedRange.Rows.Count
For RowNum = 2 To LastRow
If (Range("B" & RowNum) = Range("B" & RowNum + 1)) Then
Range("B" & RowNum).EntireRow.Clear
End If
Next RowNum
Range("A2:C" & LastRow).Sort key1:=Range("B2:B" & LastRow), _
order1:=xlAscending, Header:=xlNo
Run Code Online (Sandbox Code Playgroud) 我正在使用包裹在 lapply 语句中的 quantmod 从雅虎财经下载一些信息:
require(quantmod)
tickers <- c("AAPL", "MSFT", "MKQ", "TSLA")
quotes <- lapply(tickers,function(x) getSymbols(x, src="yahoo", from="2015-02-01", auto.assign=FALSE))
Run Code Online (Sandbox Code Playgroud)
股票代码 MKQ 是故意编造的。我希望循环打印错误,但仍然创建一个 xts 对象列表,其中包含其他 3 个代码的请求数据。
我曾尝试按如下方式使用 tryCatch 但未成功:
quotes <- tryCatch(lapply(tickers,function(x) getSymbols(x,
src="yahoo", from="2015-02-01", auto.assign=FALSE)) , error=function(e) NULL)
Run Code Online (Sandbox Code Playgroud)
关于如何做到这一点的任何建议?我阅读了有关 tryCatch 的文档,但无法理解它。
谢谢你。