我需要调用自定义函数来进行一些计算.在此函数中,有一个if语句用于检查输入值.但是我的代码没有返回我期望的值.
创建了一个测试data.frame
library(dplyr)
df <- expand.grid(x = 2:4, y = 2:4, z = 2:4)
df$value <- df$x
df <- df%>% tbl_df %>% group_by(x, y)
Run Code Online (Sandbox Code Playgroud)
test_fun1只返回所有值的总和
test_fun1 <- function(value)
{
return(sum(value))
}
df %>% summarize(t = test_fun1(value))
Run Code Online (Sandbox Code Playgroud)
test_fun1返回结果为我的预期
Source: local data frame [4 x 3]
Groups: x
x y t
1 1 1 2
2 1 2 2
3 2 1 4
4 2 2 4
Run Code Online (Sandbox Code Playgroud)
然后我添加一个if语句来检查所有值是否相等.
test_fun2 <- function(value)
{
if (all(value == 2))
{
return (NA)
}
return(sum(value))
} …Run Code Online (Sandbox Code Playgroud) 我想让用户输入一个字符串,y作为x例如的函数的公式
fn <- "x^2 + exp(3*x)"
Run Code Online (Sandbox Code Playgroud)
然后我想在某些点评估这个表达式,例如对于.的值x <- 1:0.1:100.
我知道我能做到:
x <- 1:0.1:100
y <- eval(parse(text = fn))
Run Code Online (Sandbox Code Playgroud)
但是,如果用户提供fn的不是公式,这会使我的计算机受到攻击
fn <- 'x; print("Your computer is compromised")'
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以实现我想要做的事情?
例如
chr <- c("a", "b", "c")
intgr <- c(1, 2, 3)
str(chr)
str(base::merge(chr,intgr, stringsAsFactors = FALSE))
Run Code Online (Sandbox Code Playgroud)
得到:
> str(base::merge(chr,intgr, stringsAsFactors = FALSE))
'data.frame': 9 obs. of 2 variables:
$ x: Factor w/ 3 levels "a","b","c": 1 2 3 1 2 3 1 2 3
$ y: num 1 1 1 2 2 2 3 3 3
Run Code Online (Sandbox Code Playgroud)
我原本以为它与merge如何将参数强制转换为数据框有关.但是,我认为添加参数stringsAsFactors = FALSE会覆盖char - > factor的默认强制行为,但这不起作用.
编辑:执行以下操作给出了我预期的行为:
options(stringsAsFactors = FALSE)
str(base::merge(chr,intgr))
Run Code Online (Sandbox Code Playgroud)
那是:
> str(base::merge(chr,intgr))
'data.frame': 9 obs. of 2 variables:
$ …Run Code Online (Sandbox Code Playgroud) 这里有两个相关的问题,但它们不是矿的重复与第一个有一个解决方案特定于数据集,第二个涉及的故障glm时start沿着一个被提供offset。
我有以下数据集:
library(data.table)
df <- data.frame(names = factor(1:10))
set.seed(0)
df$probs <- c(0, 0, runif(8, 0, 1))
df$response = lapply(df$probs, function(i){
rbinom(50, 1, i)
})
dt <- data.table(df)
dt <- dt[, list(response = unlist(response)), by = c('names', 'probs')]
Run Code Online (Sandbox Code Playgroud)
即dt:
> dt
names probs response
1: 1 0.0000000 0
2: 1 0.0000000 0
3: 1 0.0000000 0
4: 1 0.0000000 0
5: 1 0.0000000 0
---
496: 10 0.9446753 0
497: 10 0.9446753 …Run Code Online (Sandbox Code Playgroud) 请考虑以下示例数据
library(dplyr)
tmp <- mtcars %>%
group_by(cyl) %>%
summarise(mpg_sum = list(summary(mpg)))
Run Code Online (Sandbox Code Playgroud)
这样mpg_sum包含mpg变量的最小值,第一个四分位数,中位数,平均值,第三个四分位数和最大值cyl.
如何使用dplyr或其他方法将此列拆分为具有适当列名的6列?
我们知道,可以在R中调用函数而不将它们分配给环境,例如
> (function(x){x/2})(5)
[1] 2.5
Run Code Online (Sandbox Code Playgroud)
我想在mutate_each(或summarise_each)调用中动态使用这些函数.例如,用
df <- data.frame(a = runif(10), b = rnorm(10))
我可能会尝试执行以下操作之一,但它们都会返回错误:
library(dplyr)
> df %>%
+ mutate_each(funs((function(x){x/2})), a, b)
Error in eval(substitute(expr), envir, enclos) :
Unsupported type CLOSXP for column "a"
>
> df %>%
+ mutate_each(list((function(x){x/2})), a, b)
Error: is.fun_list(calls) is not TRUE
>
>
> df %>%
+ mutate_each(funs((function(x){x/2})(.)), a, b)
Error in vapply(dots[missing_names], function(x) make_name(x$expr), character(1)) :
values must be length 1,
but FUN(X[[1]]) result is length 2
> …Run Code Online (Sandbox Code Playgroud) 我希望这样做:从一个数据帧中获取日期并过滤另一个数据帧中的数据 - R.
除非没有加入,因为我担心在加入我的数据后,结果将太大而无法放入内存,在过滤器之前.
以下是示例数据:
tmp_df <- data.frame(a = 1:10)
Run Code Online (Sandbox Code Playgroud)
我希望做一个看起来像这样的操作:
lower_bound <- c(2, 4)
upper_bound <- c(2, 5)
tmp_df %>%
filter(a >= lower_bound & a <= upper_bound) # does not work as <= is vectorised inappropriately
Run Code Online (Sandbox Code Playgroud)
我期望的结果是:
> tmp_df[(tmp_df$a <= 2 & tmp_df$a >= 2) | (tmp_df$a <= 5 & tmp_df$a >= 4), , drop = F]
# one way to get indices to subset data frame, impractical for a long range vector
a
2 2
4 4 …Run Code Online (Sandbox Code Playgroud) 继此问题和随后的答案之后: Postgres 中 -Infinity 和 Infinity 的适当值
和文档,似乎很清楚real和double precision数字类型支持正无穷大和负无穷大。但是,没有提及numeric类型,只是range有“无限制”。
类型是否支持正无穷大和负无穷大numeric(在 PostgreSQL 9.5 中),如果是,如何插入这些值?
编辑(如@TimBiegeleisen 所建议):发生这种情况的原因是我试图将数字列从R数据库表中写入。该列包含Inf值,但使用dbWriteTablefromRPostgreSQL错误:
Error in postgresqlgetResult(new.con) :
RS-DBI driver: (could not Retrieve the result :
ERROR: invalid input syntax for type numeric: "Inf"
Run Code Online (Sandbox Code Playgroud)
在我的特殊情况下,我可以将这些值转换Inf为NA并将其写为NULL,但是当列包含缺失值或not null 在数据库中强加条件时,这不起作用。我想另一件事是写一个任意大的数字。
我的问题涉及到的distinct功能dplyr.
首先,设置数据:
set.seed(0)
df <- data.frame(
x = sample(10, 100, rep = TRUE),
y = sample(10, 100, rep = TRUE)
)
Run Code Online (Sandbox Code Playgroud)
考虑以下两种用法distinct.
df %>%
group_by(x) %>%
distinct()
df %>%
group_by(x) %>%
distinct(y)
Run Code Online (Sandbox Code Playgroud)
第一个产生第二个不同的结果.据我所知,第一组操作找到"所有不同的值x,并返回第一个值y",其中第二个找到"对于每个值x,找到所有不同的值y".
为什么会这样呢?
df %>%
distinct(x, y)
df %>% distinct()
Run Code Online (Sandbox Code Playgroud)
产生相同的结果?
编辑:看起来这已经是一个已知的错误:https://github.com/hadley/dplyr/issues/1110
出现这个问题是因为我希望为方便起见创建一个函数:
as.numeric_psql <- function(x) {
return(as.numeric(as.integer(x)))
}
Run Code Online (Sandbox Code Playgroud)
将远程 postgres 表中的布尔值转换为数字。需要转换为整数的步骤如下:
在数字和布尔值之间没有定义直接转换。您可以使用整数作为中间立场。( /sf/answers/1350347001/ )
当然,此功能在本地按预期工作:
copy_to(con_psql, cars, 'tmp_cars')
tmp_cars_sdf <-
tbl(con_psql, 'tmp_cars')
tmp_cars_sdf %>%
mutate(low_dist = dist < 5) %>%
mutate(low_dist = as.numeric(as.integer(low_dist)))
# # Source: lazy query [?? x 3]
# # Database: postgres 9.5.3
# speed dist low_dist
# <dbl> <dbl> <dbl>
# 1 4 2 1
# 2 4 10 0
# 3 7 4 1
# 4 7 22 0
# 5 8 16 0
cars %>% …Run Code Online (Sandbox Code Playgroud)