I was wondering how I could split many columns by their sign in a data.table. To be concrete, suppose that we have:
library(data.table)
DT = data.table(x = c(-1,-2,1,3),
z = c(-1,-1,-1,-1))
I am looking to create a new data.table called DT_new
such that it looks like:
DT_new
x z x_pos x_neg z_pos z_neg
1: -1 -1 0 1 0 1
2: -2 -1 0 2 0 1
3: 1 -1 1 0 0 1
4: 3 -1 3 0 0 1
The reason I am doing this is that I want to separate out the positive and negative variables in a regression. Doing a few of these manually is easy enough. But I have hundreds of variables that I want to apply this technique to. So I am hoping that there is a "SDcols" solutions.
Thanks!
CodePudding user response:
No need to use .SDcols ;-) Please find below a reprex:
- Code
DT[,`:=` (x_pos = fifelse(x>0, x, 0),
x_neg = fifelse(x<0, abs(x), 0),
z_pos = fifelse(z>0, z, 0),
z_neg = fifelse(z<0, abs(z), 0))][]
- Output
x z x_pos x_neg z_pos z_neg
1: -1 -1 0 1 0 1
2: -2 -1 0 2 0 1
3: 1 -1 1 0 0 1
4: 3 -1 3 0 0 1
AS A FOLLOW-UP TO YOUR COMMENT
Please find the reprex below.
- Code
vars <- c("x","z")
suffix <- c("_pos", "_neg")
DT[, CJ(vars, suffix, sorted = FALSE)[, paste0(vars, suffix)] := .(fifelse(x>0, x, 0),
fifelse(x<0, abs(x), 0),
fifelse(z>0, z, 0),
fifelse(z<0, abs(z), 0))][]
- Output
#> x z x_pos x_neg z_pos z_neg
#> 1: -1 -1 0 1 0 1
#> 2: -2 -1 0 2 0 1
#> 3: 1 -1 1 0 0 1
#> 4: 3 -1 3 0 0 1
Created on 2021-11-28 by the reprex package (v2.0.1)
CodePudding user response:
We could use across
with case_when
:
library(dplyr)
DT %>%
mutate(across(everything(), ~case_when(
. < 0 ~ 0,
TRUE ~ .), .names = "{col}_pos")) %>%
mutate(across(-contains("pos"), ~case_when(
. < 0 ~ abs(.),
TRUE ~ 0), .names = "{col}_neg"))
x z x_pos z_pos x_neg z_neg
1: -1 -1 0 0 1 1
2: -2 -1 0 0 2 1
3: 1 -1 1 0 0 1
4: 3 -1 3 0 0 1
CodePudding user response:
Perhaps:
library(data.table)
DT = data.table(x = c(-1,-2,1,3),
z = c(-1,-1,-1,-1))
col_nms <- c('x', 'z')
pos_nms <- paste0(col_nms, '_pos')
neg_nms <- paste0(col_nms, '_neg')
DT[, c(pos_nms) := lapply(.SD, function(.x) fifelse(.x > 0, .x, 0)), .SDcols = c('x', 'z')]
DT[, c(neg_nms) := lapply(.SD, function(.x) fifelse(.x < 0, -.x, 0)), .SDcols = c('x', 'z')]
DT
#> x z x_pos z_pos x_neg z_neg
#> 1: -1 -1 0 0 1 1
#> 2: -2 -1 0 0 2 1
#> 3: 1 -1 1 0 0 1
#> 4: 3 -1 3 0 0 1
Created on 2021-11-27 by the reprex package (v2.0.1)
CodePudding user response:
library(data.table)
DT = data.table(x = c(-1,-2,1,3),
z = c(-1,-1,-1,-1))
vars <- names(DT)
DT <- DT[, sapply(.SD, function(j){
list(ifelse(j<0, 0, j),
ifelse(j>0, 0, j))
})]
setnames(DT, paste(rep(vars, each=2), c("_pos", "_neg"), sep=""))
CodePudding user response:
Another dplyr option
library(data.table)
library(dplyr, warn.conflicts = F)
DT <- data.table(x = c(-1, -2, 1, 3),
z = c(-1, -1, -1, -1))
DT %>%
mutate(across(everything(), list(
pos = ~ if_else(. > 0, ., 0),
neg = ~ if_else(. < 0, -., 0)
)))
#> x z x_pos x_neg z_pos z_neg
#> 1: -1 -1 0 1 0 1
#> 2: -2 -1 0 2 0 1
#> 3: 1 -1 1 0 0 1
#> 4: 3 -1 3 0 0 1
Created on 2021-11-27 by the reprex package (v2.0.1)
If you want to use this syntax but do data.table operations under the hood, you can use tidytable
.
library(data.table)
library(tidytable, warn.conflicts = F)
DT <- data.table(x = c(-1, -2, 1, 3),
z = c(-1, -1, -1, -1))
DT %>%
mutate.(across.(everything(), list(
pos = ~ if_else(. > 0, ., 0),
neg = ~ if_else(. < 0, -., 0)
)))
#> # A tidytable: 4 × 6
#> x z x_pos x_neg z_pos z_neg
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 -1 -1 0 1 0 1
#> 2 -2 -1 0 2 0 1
#> 3 1 -1 1 0 0 1
#> 4 3 -1 3 0 0 1
Created on 2021-11-27 by the reprex package (v2.0.1)