A subset and replacement of rows and columns in a data table.

I need to change some columns of certain rows of a data table. I keep getting the error, "unused argument (c = F)". Does anyone know how to handle this quickly? Below is an example of using both data.frames and data.table.

Thank.

     test.df <- data.frame(a=rnorm(100, 0, 1), b=rnorm(100, 0, 1), c=rnorm(100,0,1))
     test.dt <- as.data.table(test.df)

     test.df[test.df$a<test.df$b,c(1,2)] <- 10* test.df[test.df$a<test.df$b,c(1,2)]

     test.dt[test.dt$a<test.dt$b, c(1,2), with=F] <- 10* test.dt[,c(1,2),with=F][test.dt$a<test.dt$b, c(1,2), with=F]
+4
source share
1 answer

First of all, you do not need and should not (as good programming) use the name data.tableinside [.data.table.

Secondly, you should avoid using column numbers when possible - this is the source of future headaches, and you should use column names instead.

, data.table := (. ?':=').

, , :

test.dt[a < b, `:=`(a = 10 * a, b = 10 * b)]
+8

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


All Articles