大多数代码示例显示如何将dplyr与数据库一起使用涉及创建数据库连接对象:
connStr <- "driver=driver;server=hostname;database=mydatabase;..."
db <- DBI::dbConnect(odbc::odbc(), .connection_string=connStr)
tbl <- tbl(db, "mytable")
tbl %>% verb1 %>% verb2 %>% ...
Run Code Online (Sandbox Code Playgroud)
但是,假设我省略了创建db对象:
tbl <- tbl(DBI::dbConnect(odbc::odbc(), .connection_string=connStr), "mytable")
tbl %>% verb1 %>% verb2 %>% ...
Run Code Online (Sandbox Code Playgroud)
这有什么后果吗?我会耗尽数据库资源/泄漏内存/等吗?
我想到的DBMS是SQL Server,驱动程序包是odbc,以防它重要.
考虑这两个函数:
library(Rcpp)
cppFunction("NumericVector func1(NumericVector &x)
{
for (int i = 0; i < x.length(); i++)
x[i] = x[i] * 2;
return x;
}")
cppFunction("NumericVector func2(NumericVector x) // no &
{
for (int i = 0; i < x.length(); i++)
x[i] = x[i] * 2;
return x;
}")
Run Code Online (Sandbox Code Playgroud)
唯一的区别是,func1将其x作为参考参数,而func2将其作为值。如果这是常规的 C++,我会将其理解为func1允许更改调用代码中的值x,而这不会在func2.
然而:
> x <- 1:10/5 # ensure x is numeric, not integer
> x
[1] 0.2 0.4 0.6 0.8 …Run Code Online (Sandbox Code Playgroud) 我尝试做类似的事情
df[<very-long-and-complicated-selection>,]$foo <- "bar"
Run Code Online (Sandbox Code Playgroud)
如果存在与选择匹配的行,则此方法很有效.
如果没有,我收到错误消息
$<-.data.frame(*tmp*,"foo",value ="bar")出错:
替换有1行,数据有0
但是,我的代码是以一种不可能匹配的方式设计的.
是否有一个干净,简短的解决方案来避免这些(并且只有这些)错误?
Is it possible to count a repeating part of a sequence in R? For example:
x<- c(1,3.0,3.1,3.2,1,1,2,3.0,3.1,3.2,4,4,5,6,5,3.0,3.1,3.2,
3.1,2,1,4,6,4.0,4,3.0,3.1,3.2,5,3.2,3.0,4)
Run Code Online (Sandbox Code Playgroud)
Is it possible to count the times that the subsequence 3.0,3.1,3.2 occurs? So in this example it must be: 4
我dopers在R中读一个csv文件" ".
dopers <- read.csv(file="generalDoping_alldata2.csv", head=TRUE,sep=",")
Run Code Online (Sandbox Code Playgroud)
读完文件后,我必须做一些数据清理.例如,country如果它在列中
"美国"或"美国"
我想用它替换它 "USA"
我想确保,如果单词是" United States "或者"United State ",即使他们我的代码应该工作.我想说的是,即使在"United States"替换之前和之后有任何字符"USA".我知道我们可以sub()为此目的使用功能.我在网上找到了这个,但我不明白是什么"^" "&" "*" ".".有人可以解释一下.
dopers$Country = sub("^UNITED STATES.*$", "USA", dopers$Country)
Run Code Online (Sandbox Code Playgroud) 我想了解如何将表示表达式的字符串传递给dplyr,以便将字符串中提到的变量计算为数据帧中列的表达式.关于这个主题的主要内容包括传递,并且根本不讨论字符串.
很明显,在表示表达式时,quosures比字符串更安全,更清晰,所以当使用quosures时我们当然应该避免使用字符串.但是,在使用R生态系统之外的工具(例如javascript或YAML配置文件)时,通常需要使用字符串而不是quosures.
例如,假设我想要一个使用用户/调用者传入的表达式进行分组计数的函数.正如预期的那样,以下代码不起作用,因为dplyr使用非标准求值来解释参数group_by.
library(tidyverse)
group_by_and_tally <- function(data, groups) {
data %>%
group_by(groups) %>%
tally()
}
my_groups <- c('2 * cyl', 'am')
mtcars %>%
group_by_and_tally(my_groups)
#> Error in grouped_df_impl(data, unname(vars), drop): Column `groups` is unknown
Run Code Online (Sandbox Code Playgroud)
在dplyr 0.5中,我们将使用标准评估group_by_(.dots = groups)来处理这种情况.既然下划线动词已弃用,我们应该如何在dplyr 0.7中执行此类操作?
在只是列名的表达式的特殊情况下,我们可以使用这个问题的解决方案,但它们不适用于更复杂的表达式,例如2 * cyl不仅仅是列名.
我正在尝试编写一个函数,它将来自用户的列名称向量作为其参数之一.列名将用于指定将数据帧的哪些列粘贴在一起以在dplyr :: mutate中形成新列.我试图首先折叠参数向量的元素,然后在mutate中使用折叠的字符串 - 这是错误的.请参阅下面的最新尝试.我做了其他尝试,但我不理解dplyr中的新quo,enquo,UQ,!!!,!!等等.有人可以展示我需要做什么吗?
df <- data.frame(.yr = c("2000", "2001", "2002"), .mo = c("12", "01", "02"), .other = rnorm(3))
cols <- colnames(df)[1:2]
do_want <- df %>%
mutate(new = paste(.yr, .mo, sep = "-"))
my_func <- function(dat, vars){
.vars <- paste(vars, collapse = ",")
result <- dat %>%
mutate(new = paste(.vars, sep = "-" ))
return(result)
}
my_func(dat = df, vars = cols)
Run Code Online (Sandbox Code Playgroud)
编辑:这是我尝试使用quo和!! 在函数定义中.结果是一列重复的字符串".yr,.mo"
my_func <- function(dat, vars){
.vars <- quo(paste(vars, collapse = ","))
result <- dat %>%
mutate(new …Run Code Online (Sandbox Code Playgroud) 如何在 R 中获取当前系统时间?例如,使用时Sys.time()
我得到这样的信息:
2021-04-14 13:04:27
但我只想要时间部分 13:04:27
我对p值有疑问.我一直在比较不同的线性模型,以确定一个模型是否比另一个更好,在R中具有以下功能.
anova(model1,model2)
Run Code Online (Sandbox Code Playgroud)
不幸的是,偶尔它不会计算F或p值.这是一个没有给出p值的anova摘要的例子
Analysis of Variance Table
Model 1: Influence ~ SortedSums[, Combos2[1, A]] + SortedSums[, Combos2[2,A]]
Model 2: Influence ~ SortedSums[, B]
Res.Df RSS Df Sum of Sq F Pr(>F)
1 127 3090.9
2 128 2655.2 -1 435.74
Run Code Online (Sandbox Code Playgroud)
为了对称性,这里也是一个产生p值的anova总结.
Analysis of Variance Table
Model 1: Influence ~ SortedSums[, Combos2[1, A]] + SortedSums[, Combos2[2,A]]
Model 2: Influence ~ SortedSums[, B]
Res.Df RSS Df Sum of Sq F Pr(>F)
1 127 3090.9
2 128 3157.6 -1 -66.652 2.7386 0.1004
Run Code Online (Sandbox Code Playgroud)
你知道为什么会这样吗?
我想运行R版本3.4而不是当前版本3.3.2,并且不知道从哪里开始.
尽管如此,网络搜索仍然很少,因为我不习惯在Windows 10平台上工作,也不完全了解Visual Studio中的组件是如何分层的.