How to change the type of the goal column during execution: = by groups in the data table. R?

I am trying to do: = by group for an existing column of type "integer", where the new values ​​are of type "double", which fails.

My script mutates a column representing time in POSIXct, based on values ​​in other columns. I could change the creation of data.table as work, but I'm still interested in how to change the type of the column as suggested in the error message.

Here is a simple example of my problem:

db = data.table(id=rep(1:2, each=5), x=1:10, y=runif(10)) db id xy 1: 1 1 0.47154470 2: 1 2 0.03325867 3: 1 3 0.56784494 4: 1 4 0.47936031 5: 1 5 0.96318208 6: 2 6 0.83257416 7: 2 7 0.10659533 8: 2 8 0.23103810 9: 2 9 0.02900567 10: 2 10 0.38346531 db[, x:=mean(y), by=id] Error in `[.data.table`(db, , `:=`(x, mean(y)), by = id) : Type of RHS ('double') must match LHS ('integer'). To check and coerce would impact performance too much for the fastest cases. Either change the type of the target column, or coerce the RHS of := yourself (eg by using 1L instead of 1) 
+6
source share
1 answer

We can convert the column class "x" to "numeric" before assigning "mean (y)" to "x" because the class "x" is an "integer". This can be useful if we replace β€œx” with mean any other numeric variable (including β€œx”).

 db[, x:= as.numeric(x)][, x:= mean(y), by=id][] 

Or assign a new column and subsequently change the column name

 setnames(db[, x1:= mean(y),by=id][,x:=NULL],'x1', 'x') 

Or we can set 'x' to 'NULL' and then create 'x' as mean of 'y' (@David Arenburg proposal)

 db[, x:=NULL][, x:= mean(y), by= id][] 
+10
source

Source: https://habr.com/ru/post/985163/


All Articles