colon-equals

When should I use the := operator in data.table?

主宰稳场 提交于 2019-11-26 04:36:30
问题 data.table objects now have a := operator. What makes this operator different from all other assignment operators? Also, what are its uses, how much faster is it, and when should it be avoided? 回答1: Here is an example showing 10 minutes reduced to 1 second (from NEWS on homepage). It's like subassigning to a data.frame but doesn't copy the entire table each time. m = matrix(1,nrow=100000,ncol=100) DF = as.data.frame(m) DT = as.data.table(m) system.time(for (i in 1:1000) DF[i,1] <- i) user

Assign multiple columns using := in data.table, by group

穿精又带淫゛_ 提交于 2019-11-26 01:51:40
问题 What is the best way to assign to multiple columns using data.table ? For example: f <- function(x) {c(\"hi\", \"hello\")} x <- data.table(id = 1:10) I would like to do something like this (of course this syntax is incorrect): x[ , (col1, col2) := f(), by = \"id\"] And to extend that, I may have many columns with names stored in a variable (say col_names ) and I would like to do: x[ , col_names := another_f(), by = \"id\", with = FALSE] What is the correct way to do something like this? 回答1:

The forgotten assignment operator “=” and the commonplace “:=”

老子叫甜甜 提交于 2019-11-26 00:15:50
问题 The documentation for PL/pgSQL says, that declaration and assignment to variables is done with := . But a simple, shorter and more modern (see footnote) = seems to work as expected: CREATE OR REPLACE FUNCTION foo() RETURNS int AS $$ DECLARE i int; BEGIN i = 0; WHILE NOT i = 25 LOOP i = i + 1; i = i * i; END LOOP; RETURN i; END; $$ LANGUAGE plpgsql; > SELECT foo(); 25 Please note, that Pl/pgSQL can distinguish assignment and comparison clearly as shown in the line WHILE NOT i = 25 LOOP So, the