这个问题相当于Pass a data.frame column name to a function 的data.table 问题。
假设我有一个非常简单的 data.table:
dat <- data.table(x = 1:4,
y = 5:8)
Run Code Online (Sandbox Code Playgroud)
现在我想为任何给定的函数创建一个新列:
new_column <- function(df,col_name,expr){
col_name <- deparse(substitute(col_name))
df[[col_name]] <- eval(substitute(expr),df,parent.frame())
df
}
Run Code Online (Sandbox Code Playgroud)
以便它正确地提供:
> new_column (dat,z,x+y)
x y z
1 1 5 6
2 2 6 8
3 3 7 10
4 4 8 12
Run Code Online (Sandbox Code Playgroud)
但是,因为它是一个 data.table,我想使用以下方法创建这个新列:=:
new_column_byref <- function(df,col_name,expr){
col_name <- deparse(substitute(col_name))
df[, col_name:=eval(substitute(expr)
,df
,parent.frame()
)]
df
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用:
> a <- new_column_byref(dat,z,x+y)
Error: …Run Code Online (Sandbox Code Playgroud) 创建一组两个图形时,在 lapply 循环内打印将在 RStudio 绘图面板中打印两次。
x=1:7
y=1:7
df1 = data.frame(x=x,y=y)
x=10:70
y=10:70
df2 = data.frame(x=x,y=y)
db <- list(df1, df2)
# Given a data frame, the function below creates a graph
create.graph <- function (df){
p <- ggplot(df,aes(x,y))+geom_point()
# here goes other stuff, such as ggsave()
return (p)
}
# collect.graph is a list of generated graphs
collect.graph <- lapply(db,create.graph)
# Finally, lapply prints the list of collected graphs
lapply(collect.graph,print)
Run Code Online (Sandbox Code Playgroud)
该代码工作正常,但它在 RStudio 中生成两组图形,而不是一组。
如何避免这种行为?
我的目标是在 data.table 中取消嵌套列。原始data.table有80万多行,下面的链接 有5k行的样本。
然而,我注意到取消嵌套这个数据集所需的时间随着行数的平方增长,而不是像我预期的那样大致呈线性方式:
# Subset for 500 rows
> item_res <- item[1:500]
> microbenchmark(item_res[, lance[[1]], by = item_id], times = 5L)
Unit: milliseconds
expr min lq mean median uq max neval
item_int <- item_res[, lance[[1]], by = item_id] 281.3878 282.2426 286.9925 284.4111 286.1291 300.792 5
# Subset for 5000 rows
> item_res <- item[1:5000]
> microbenchmark(item_res[, lance[[1]], by = item_id], times = 5L)
Unit: seconds
expr min lq mean median uq max neval
item_int <- item_res[, lance[[1]], …Run Code Online (Sandbox Code Playgroud) 我想计算在整洁的 data.table 中每个组有多少个 TRUE 标记:
DT <- data.table( id = c(1 ,1 ,1 ,2 ,2 ,2 ,2 ,2 )
, marker = c(TRUE,FALSE,FALSE,TRUE,FALSE,TRUE,TRUE,FALSE))
Run Code Online (Sandbox Code Playgroud)
所以我尝试了DT[marker==TRUE, num_markers := .N, by = id],输出:
id marker num_markers
1: 1 TRUE 1
2: 1 FALSE NA
3: 1 FALSE NA
4: 2 TRUE 3
5: 2 FALSE NA
6: 2 TRUE 3
7: 2 TRUE 3
8: 2 FALSE NA
Run Code Online (Sandbox Code Playgroud)
相反,所需的输出是:
id marker num_markers
1: 1 TRUE 1
2: 1 FALSE 1
3: …Run Code Online (Sandbox Code Playgroud) 当在 data.table 中按组计算平均值时,我得到不同的结果:
\nqty <- c(1:6)\nname <- c("a", "b","a", "a", "c","b" )\ntype <- c("i", "i", "i", "f", "f", "f")\n\nDT <- data.table(qty,name,type) \n\nDT[, avg_mean := mean(qty) , by = .(name, type)]\nDT[, avg_sum_N := sum(qty)/.N , by = .(name, type)]\n\n > DT\n qty name type avg_mean avg_sum_N\n <int> <char> <char> <num> <num>\n1: 1 a i 2 2\n2: 2 b i 4 2\n3: 3 a i 2 2\n4: 4 a f 2 4\n5: 5 c f 6 5\n6: 6 b f 5 …Run Code Online (Sandbox Code Playgroud) 我需要从 data.table 中删除列包含向量中嵌套的a任何行:NA
library(data.table)
a = list(as.numeric(c(NA,NA)), 2,as.numeric(c(3, NA)), c(4,5) )
b <- 11:14
dt <- data.table(a,b)
Run Code Online (Sandbox Code Playgroud)
因此,应删除第 1 行和第 3 行。
我尝试了三种解决方案但没有成功:
dt1 <- dt[!is.na(a)]
dt2 <- dt[!is.na(unlist(a))]
dt3 <- dt[dt[,!Reduce(`&`, lapply(a, is.na))]]
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢。
我正在尝试在图中绘制两个“ geom_vline()”。
下面的代码对于一条垂直线工作正常:
x=1:7
y=1:7
df1 = data.frame(x=x,y=y)
vertical.lines <- c(2.5)
ggplot(df1,aes(x=x, y=y)) +
geom_line()+
geom_vline(aes(xintercept = vertical.lines))
Run Code Online (Sandbox Code Playgroud)
但是,当我通过更改添加第二条所需的垂直线时
vertical.lines <- c(2.5,4),我得到了错误:
´Error: Aesthetics must be either length 1 or the same as the data (7): xintercept´
Run Code Online (Sandbox Code Playgroud)
我该如何解决?