我安装了带有SSDT的 VS2015 ,以及 SSMS和SqlServer PowerShell模块(包括invoke-sqlcmd
命令),但是如果我尝试对Azure SQL数据仓库执行查询,如下所示:
invoke-sqlcmd -Query "Select top 5 * from customer" -ConnectionString "Server=tcp:my.database.windows.net,1433;Database=Customer; Authentication=Active Directory Integrated; Encrypt=True; "
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
invoke-sqlcmd : Unable to load adalsql.dll (Authentication=ActiveDirectoryIntegrated). Error code: 0x2. For more information, see
http://go.microsoft.com/fwlink/?LinkID=513072
At line:1 char:1
+ invoke-sqlcmd -Query "Select top 5 * from vwOffer" -ConnectionStrin ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlException
+ FullyQualifiedErrorId : SqlExectionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
invoke-sqlcmd :
At line:1 char:1
+ invoke-sqlcmd -Query "Select top 5 …
Run Code Online (Sandbox Code Playgroud) 我使用以下习惯来有条件地从data.frame中选择列:
DF = data.frame(a = 1:3,b = letters[1:3],c = LETTERS[1:3])
someCondition <- FALSE
# use `if(someCondition)` to conditionally include column 'c'
DF[,c('a','b',if(someCondition)'c')]
:> a b
:> 1 1 a
:> 2 2 b
:> 3 3 c
Run Code Online (Sandbox Code Playgroud)
但是等价物不适用于data.table的b/c NULL值不会从列表中删除,就像它们从连接中删除一样:
DT = as.data.table(DF)
DT[,.(a,b,if(someCondition)c)]
:> Error in setnames(jval, jvnames) :
:> Can't assign 3 names to a 2 column data.table
Run Code Online (Sandbox Code Playgroud)
我已经定义了一个函数..
,它是一个解决方法:
.. <- function(...){
x = list(...)
x= x[!sapply(x,is.null)]
x
}
DT[,..(a,b,if(someCondition)c)]
:> V1 V2
:> 1: 1 a …
Run Code Online (Sandbox Code Playgroud) 在试图理解'percent_rank'的作用时,我看了一眼代码并找到了表达式length(!is.na(x))
.我想不出任何情况,length(x) != length(!x)
所以我想知道这是否是一个错字(也许它应该是sum(!is.na(x))
?)或者如果真的是这样的情况?
更新:看来这是data.table版本1.9.4的问题,而不是软件包的最新版本(在撰写本文时为1.9.6)。
我有一个通过fread
如下方式读取的表:
library(data.table)
library(bit64)
dt = fread('"x","y"\n2489751247,"a"\n2492940518,"b"\n2444706811,"a"\n2408767228,"b"')
:> x y
:> 1: 2489751247 a
:> 2: 2492940518 b
:> 3: 2444706811 a
:> 4: 2408767228 b
Run Code Online (Sandbox Code Playgroud)
并且我想要x
条件on 的总和y
,但是data.table给出了错误的答案:
dt[,.(total=sum(x)),by=y]
:> y total
:> 1: a 2.437946e-314
:> 2: b 2.421765e-314
Run Code Online (Sandbox Code Playgroud)
没有警告消息的礼貌。事实证明x是integer64类的:
lapply(dt,class)
:> $x
:> [1] "integer64"
:> $y
:> [1] "character"
Run Code Online (Sandbox Code Playgroud)
所以我可以像这样手动进行s3调度:
dt[,.(total=sum.integer64(x)),by=y]
:> y total
:> 1: a 4934458058
:> 2: b 4901707746
Run Code Online (Sandbox Code Playgroud)
出于某种原因,在j
子句中使用x的类会使data.table给出正确的答案:
dt[,.(total=sum(x),cls=class(x)),by=y]
:> y total cls …
Run Code Online (Sandbox Code Playgroud) 给定两个数据表(tbl_A
和tbl_B
),我想选择 中tbl_A
具有匹配行的所有行tbl_B
,并且我希望代码具有表现力。如果%in%
为 data.tables 定义了该运算符,那么像这样的操作将是理想的:
subset <- tbl_A[tbl_A %in% tbl_B]
Run Code Online (Sandbox Code Playgroud)
我可以想到很多方法来实现我想要的,例如:
# double negation (set differences)
subset <- tbl_A[!tbl_A[!tbl_B,1,keyby=a]]
# nomatch with keyby and this annoying `[,V1:=NULL]` bit
subset <- tbl_B[,1,keyby=.(a=x)][,V1:=NULL][tbl_A,nomatch=0L]
# nomatch with !duplicated() and setnames()
subset <- tbl_B[!duplicated(tbl_B),.(x)][tbl_A,nomatch=0L]; setnames(subset,"x","a")
# nomatch with !unique() and setnames()
subset <- unique(tbl_B)[,.(x)][tbl_A,nomatch=0L]; setnames(subset,"x","a")
# use of a temporary variable (Thanks @Frank)
subset <- tbl_A[, found := FALSE][tbl_B, found := TRUE][(found)][,found:=NULL][]
Run Code Online (Sandbox Code Playgroud)
但每个表达式都很难阅读,而且乍一看代码在做什么并不明显。有没有更惯用/更具表现力的方式来完成这项任务? …
我想使用MySQL数据库,dplyr
而无需将我的数据库密码存储在平面文本R代码中.因此,我更喜欢引用我的.my.cnf
文件,但由于src_mysql具有主机,用户和密码的默认参数,我可以找到的唯一方法是通过相当优雅:
test_db <- src_mysql("test",
default.file=path.expand("~/.my.cnf"),
host=NULL,
user=NULL,
password=NULL)
Run Code Online (Sandbox Code Playgroud)
是否有一种不那么冗长的方式从dplyr
存储凭证连接到MySQL数据库?
我正在尝试在 R 中实现嵌套的 tryCatch 。代码如下:
tryCatch({
a <- 2/'a'
print(a)
print("himanshu")
tryCatch({
a <- 3/'a'
print(a)
print("inner loop")
}, warning = function(war) {
print("Warning in inner tryCAtch")
}, error = function(err) {
print("Error in inner tryCAtch")
})
}, warning = function(war) {
print("Warning in outer tryCAtch")
}, error = function(err) {
print("Error in outer tryCAtch")
})
Run Code Online (Sandbox Code Playgroud)
即使内部块中有错误,外部块的消息也会被打印。我想要的输出如下:
Error in inner block
Error in outer block
Run Code Online (Sandbox Code Playgroud) 我有如下 NodeJS 代码:
[myFile.js]
var path = require("path");
var cp = require("child_process");
var child = cp.spawn( "python",["HelloWorld.py"], { stdio:'pipe', });
child.stdout.on('data', (data) => {
console.log(`CHILD stdout: ${data }`);
});
child.stderr.on('data', (data) => {
console.log(`CHILD stderr: ${data}`);
});
process.on("SIGINT",()=>{
// close gracefully
console.log("Received SIGINT");
})
child.on('exit',(code)=>{console.log(`child Process exited with code ${code}`)})
Run Code Online (Sandbox Code Playgroud)
和 python 脚本如下:
[HelloWorld.py]
print 'Hi there'
import time
time.sleep(5)
Run Code Online (Sandbox Code Playgroud)
我想优雅地管理关闭,但是当我通过以下方式在命令行启动此代码时:
> node myFile.js
Run Code Online (Sandbox Code Playgroud)
然后按 Control-C,我将其打印到控制台:
^CReceived SIGINT
CHILD stdout: Hi there
CHILD stderr: Traceback (most recent call last):
File …
Run Code Online (Sandbox Code Playgroud) 我有以下时间序列(data.in.从2012-01-01开始,到2012-12-31结束):
ts.rmean ts.rmax
2012-01-01 3.163478 5.86
2012-01-02 3.095909 4.67
2012-01-03 3.112000 6.01
2012-01-04 2.922800 5.44
2012-01-05 2.981154 5.21
2012-01-06 3.089167 5.26
Run Code Online (Sandbox Code Playgroud)
我正在使用dygraph for R进行绘图:
library(dplyr)
library(digraphs)
dygraph(data.in, main = "Title") %>%
dySeries("ts.rmean", drawPoints = TRUE, color = "blue") %>%
dySeries("ts.rmax", stepPlot = TRUE, fillGraph = TRUE, color = "red") %>%
dyHighlight(highlightSeriesOpts = list(strokeWidth = 3))
Run Code Online (Sandbox Code Playgroud)
可以在x轴上显示月份而不是月份年份(例如"1月12日")?而且,在情节的传说中,而不是显示月日(例如2012年1月1日)以显示月日?
用data.table
's的纠缠数据,我发现自己写了很多这样的第三行:
DT = data.table(a = 1:10)
name = 'a'
DT[,eval(parse(text=sprintf('%s_plus_one := %s + 1',name,name)))]
Run Code Online (Sandbox Code Playgroud)
我曾希望减少到
DT[,s('%s_plus_one := %s + 1',name,name)]
Run Code Online (Sandbox Code Playgroud)
使用如下函数:
# s is *very* short for substitute and evalute
s <- function(...)
eval(parse(text=sprintf(...)))
Run Code Online (Sandbox Code Playgroud)
但后来我收到这个错误:
> DT[,s('%s_plus_one := %s + 1',name,name)]
Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(":=").
Run Code Online (Sandbox Code Playgroud)
我知道我可以这样做:
# sp is short for substitute and parse
sp <- function(...)
parse(text=sprintf(...))
DT[,eval(sp('%s_plus_one …
Run Code Online (Sandbox Code Playgroud) 我需要将一些数据从一个数据合并data.table
到另一个数据中。我知道如何data.table
在联接中将新列从一个列添加到另一个列。以下列b
中的值df2
将添加到df1
基于该Id
列的联接中:
df1 <- data.table(Id = letters[1:5], a = 1:5)
df2 <- data.table(Id = letters[1:3], a = 7:9, b = 7:9)
setkey(df1, Id)
setkey(df2, Id)
df1[df2, b := b][]
#> Id a b
#> 1: a 1 7
#> 2: b 2 8
#> 3: c 3 9
#> 4: d 4 NA
#> 5: e 5 NA
Run Code Online (Sandbox Code Playgroud)
df1
但是,当列已存在于 中(此处为 列 )时,该习惯用法不起作用a
:
df1[df2, a := a][] …
Run Code Online (Sandbox Code Playgroud) 我想使用optimize()
或类似的东西来搜索函数的最小值/最大值.但是我不确定函数应该优化的确切范围,这是函数'optimze()'的必需参数(例如optimize(f=FUN,interval=c(lowerBound,upperBound))
).
在这个优化问题中,我能够估计一个a
与最优解决方案"接近"的值,但"接近度"取决于具体情况.
R中是否有一个函数可以使用初始值a
,该值不需要预先指定优化函数的时间间隔?
r ×10
data.table ×5
dplyr ×2
dygraphs ×1
eval ×1
join ×1
node.js ×1
optimization ×1
rmysql ×1
spawn ×1
sql-server ×1
stdin ×1