data.table

evaluate expression in data.table

孤街醉人 提交于 2021-02-10 18:33:36
问题 I'm trying to evaluate a string as a formula: In dplyr it would look like this: dt = data.table(a = 1:10) expr = 'sum(a)' dt %>% mutate(b := !!parse_expr(expr)) However when I try with data.table I'm getting an error: dt[, b := parse_expr(expr)] Error in [.data.table (dt, , := (b, parse_expr(expr))) : RHS of assignment is not NULL, not an an atomic vector (see ?is.atomic) and not a list column. 回答1: Instead of parse_expr , eval(parse can be used dt[, b := eval(parse(text = expr))] Or wrap

R: Creating a new row based on previous rows

雨燕双飞 提交于 2021-02-10 15:55:59
问题 I'm new to R and trying to create a new row based on values on previous rows. Sample data: df <- data.table(Item = c("a", "b", "c", "d"), "2010FY" = c(3, 5, 2, 2), "2011FY" = c(5, 6, 2, 1), "2012FY" = c(-1, 2, 2, 0.5)) I would like to create a new row that divides the 3rd row by the 4th row. Let's call this Item "e" and ideally should like this: Item 2010FY 2011FY 2012FY 1 a 3 5 -1 2 b 5 6 2 3 c 2 2 2 4 d 2 1 0.5 5 e 1 2 4 Using dplyr, my first attempt is: df <- bind_rows(df, e = df[Item %in%

R: Creating a new row based on previous rows

こ雲淡風輕ζ 提交于 2021-02-10 15:55:02
问题 I'm new to R and trying to create a new row based on values on previous rows. Sample data: df <- data.table(Item = c("a", "b", "c", "d"), "2010FY" = c(3, 5, 2, 2), "2011FY" = c(5, 6, 2, 1), "2012FY" = c(-1, 2, 2, 0.5)) I would like to create a new row that divides the 3rd row by the 4th row. Let's call this Item "e" and ideally should like this: Item 2010FY 2011FY 2012FY 1 a 3 5 -1 2 b 5 6 2 3 c 2 2 2 4 d 2 1 0.5 5 e 1 2 4 Using dplyr, my first attempt is: df <- bind_rows(df, e = df[Item %in%

How to subset data by filtering and grouping efficiently in R

僤鯓⒐⒋嵵緔 提交于 2021-02-10 05:09:26
问题 I'm working on a project and am looking for some help to make my code run more efficiently. I've searched for similar problems but can't seem to find anything quite as granular as this one. The solution I've come up with is extremely clunky. I'm confident that there must be a more efficient way to do this with a package like dplyr , data.tables , etc. Problem: I have 3 columns of data, 'ids' , 'x.group' , and 'times' . I need to extract the first 3 unique 'ids' that appear in each 'times'

.SD and .SDcols for the i expression in data.table join

≯℡__Kan透↙ 提交于 2021-02-10 02:57:09
问题 i'm trying to copy a subset of columns from Y to X based on a join, where the subset of columns is dynamic I can identify the columns quite easily: names(Y)[grep("xxx", names(Y))] but when i try to use that code in the j expression, it just gives me the column names, not the values of the columns. the .SD and .SDcols gets pretty close, but they only apply to the x expression . I'm trying to do something like this: X[Y[names(Y)[grep("xxx", names(Y))] := .SD, .SDcols = names(Y)[grep("xxx",

.SD and .SDcols for the i expression in data.table join

别说谁变了你拦得住时间么 提交于 2021-02-10 02:56:06
问题 i'm trying to copy a subset of columns from Y to X based on a join, where the subset of columns is dynamic I can identify the columns quite easily: names(Y)[grep("xxx", names(Y))] but when i try to use that code in the j expression, it just gives me the column names, not the values of the columns. the .SD and .SDcols gets pretty close, but they only apply to the x expression . I'm trying to do something like this: X[Y[names(Y)[grep("xxx", names(Y))] := .SD, .SDcols = names(Y)[grep("xxx",

.SD and .SDcols for the i expression in data.table join

一笑奈何 提交于 2021-02-10 02:55:57
问题 i'm trying to copy a subset of columns from Y to X based on a join, where the subset of columns is dynamic I can identify the columns quite easily: names(Y)[grep("xxx", names(Y))] but when i try to use that code in the j expression, it just gives me the column names, not the values of the columns. the .SD and .SDcols gets pretty close, but they only apply to the x expression . I'm trying to do something like this: X[Y[names(Y)[grep("xxx", names(Y))] := .SD, .SDcols = names(Y)[grep("xxx",

Unexpected error message while joining data.table with rbindlist() using mget()

非 Y 不嫁゛ 提交于 2021-02-09 15:22:15
问题 While preparing this answer, I've got the error message Error: value for ‘spine_hlfs’ not found from running setDT(giraffe)[rbindlist(mget(df_names), idcol = "df.name"), on = "runkey", project := df.name][] while df_list <- mget(df_names) setDT(giraffe)[rbindlist(df_list, idcol = "df.name"), on = "runkey", project := df.name][] works as expected. Before reporting an issue on github, I want to verify with the community that this indeed is a bug or if there is a simple explanation for the error

Unexpected error message while joining data.table with rbindlist() using mget()

☆樱花仙子☆ 提交于 2021-02-09 15:16:44
问题 While preparing this answer, I've got the error message Error: value for ‘spine_hlfs’ not found from running setDT(giraffe)[rbindlist(mget(df_names), idcol = "df.name"), on = "runkey", project := df.name][] while df_list <- mget(df_names) setDT(giraffe)[rbindlist(df_list, idcol = "df.name"), on = "runkey", project := df.name][] works as expected. Before reporting an issue on github, I want to verify with the community that this indeed is a bug or if there is a simple explanation for the error

Unexpected error message while joining data.table with rbindlist() using mget()

房东的猫 提交于 2021-02-09 15:14:20
问题 While preparing this answer, I've got the error message Error: value for ‘spine_hlfs’ not found from running setDT(giraffe)[rbindlist(mget(df_names), idcol = "df.name"), on = "runkey", project := df.name][] while df_list <- mget(df_names) setDT(giraffe)[rbindlist(df_list, idcol = "df.name"), on = "runkey", project := df.name][] works as expected. Before reporting an issue on github, I want to verify with the community that this indeed is a bug or if there is a simple explanation for the error