这个问题与一篇类似的帖子有关.函数写入将列引用传递给group_by
但是,我想将几个输入传递给使用group_by_()和summarise_()的函数.
这是我的功能:
foo <- function(data,column,x1,x2){
data %>%
group_by_(.dots = column) %>%
summarise_(.dots= c(~mean(x1), ~mean(x2)))
}
Run Code Online (Sandbox Code Playgroud)
但是,当我跑
foo(mtcars,"cyl", "disp", "hp")
Run Code Online (Sandbox Code Playgroud)
我收到以下错误.
Error in UseMethod("as.lazy_dots") :
no applicable method for 'as.lazy_dots' applied to an object of class "c('double', 'numeric')"
In addition: Warning message:
In mean.default(x1) : argument is not numeric or logical: returning NA
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我做错了什么?
我想计算"sp"列中所有变量的滚动平均值.这是我的数据样本:
the_date sp wins
01-06--2012 1 305
02-06--2012 1 276
03-06--2012 1 184
04-06--2012 1 248
05-06--2012 1 243
06-06--2012 1 363
07-06--2012 1 272
01-06--2012 2 432
02-06--2012 2 369
03-06--2012 2 302
04-06--2012 2 347
05-06--2012 2 357
06-06--2012 2 331
07-06--2012 2 380
01-06--2012 3 1
02-06--2012 3 2
03-06--2012 3 3
04-06--2012 3 2
05-06--2012 3 0
06-06--2012 3 2
07-06--2012 3 0
Run Code Online (Sandbox Code Playgroud)
我想要的是将一列添加到数据中,为每个sp提供超过3天的移动平均值.所以我想要的是以下输出:
the_date sp wins SMA_wins
01-06--2012 1 305 305.00
02-06--2012 1 276 290.50 …Run Code Online (Sandbox Code Playgroud) 我有一个字符串列表。
date_str = ["2012-11-04 1:05:21", "2013-11-03 1:05:21", "2014-11-02 1:07:31"]
Run Code Online (Sandbox Code Playgroud)
我想将它们作为日期时间对象来读取。对于一根弦,我做
datetime.strptime(date_str[1], "%Y-%m-%d %H:%M:%S")
Run Code Online (Sandbox Code Playgroud)
但我不知道如何创建日期时间对象列表。如果我使用
datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
TypeError: must be string, not list
Run Code Online (Sandbox Code Playgroud)
我尝试使用循环:
x = datetime.strptime(date_str[0], "%Y-%m-%d %H:%M:%S")
for i in range(1,len(date_str)):
x.append(datetime.strptime(date_str[i], "%Y-%m-%d %H:%M:%S"))
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误
AttributeError: 'datetime.datetime' object has no attribute 'append'
Run Code Online (Sandbox Code Playgroud)
有没有办法创建日期时间对象列表?
在R中是否有一个不同的函数用于获得与SQL相同的左或右函数结果?
例如,SQL中的以下查询将给出列的前6个字符:
select left(x, 6)
from table
Run Code Online (Sandbox Code Playgroud)
但是,当我在sqldf中尝试这样的时候:
sqldf("select left(x,6) from table")
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
sqliteSendQuery(con,statement,bind.data)中的错误:
语句中的错误:接近"(":语法错误
它给了我x变量但不是左(x,6).为了澄清,x的长度大于6.
我有一个标题,后跟一个Rmd文件中的代码块.如果满足条件,我只想包含此标题和后跟它的块.我知道如何使用块,因为它在代码的主体中,但我如何做前者?
```{r}
print_option <- TRUE
```
## My header
```{r}
if(print_option==TRUE) {
print (x)
}
```
Run Code Online (Sandbox Code Playgroud)