Description Usage Arguments Examples
If dt is a data table, then modify is essentially just a wrapper for data.table syntax that allows modification or creation of new columns. If dt is not a data table, it will by default be converted to a data table and then transformed and returned as the original data frame. Unlike mutate from dplyr, one can use the .SD argument in function calls, which can quite useful sometimes.
1 2 | modify(.dt, .if, .by = NULL, ..., .envir = parent.frame(),
.inplace = is.data.table(.dt), .as.data.table = is.data.table(.dt))
|
.dt |
a data.table |
.if |
optional a boolean conditions that specifies the rows that shall be modifed |
.by |
optional a vector of column names used for computations that are splitted by groups |
... |
formulas for columns that are modified or newly created |
.envir |
optional an environment in which the expressions shall be evaluated if variables are not found in .dt |
.inplace |
allows .dt inplace modification (TRUE if .dt is a data table) |
.as.data.table |
shall result be a data.table (only true if .dt is a data.table) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | ## Not run:
library(microbenchmark)
K = 3
n = 10
dt = data.table(a= sample(1:3,n,replace=TRUE),
b= sample(1:100,n,replace=TRUE),
x=rnorm(n))
df = as.data.frame(dt)
# Set x to 100 where a==2
modify(dt,a==2, y=x+100, z=y+K)
modify(df,a==2,y=200,z=y*5+K)
dt[,y:=x+2]
# Set x to the mean value of b*100 in each group of a
modify(dt,.by=c("a"), x=mean(b)*100)
dt
# Call with strings
com = "x=200"
s_modify(dt,"a==2", com)
dt
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.