MYaseen208 MYaseen208 - 3 months ago 14
R Question

data.table Splitting column into multiple columns based on two conditions

I want to split

Y
column of the following
data.table
into two columns based on
/
or
-
. Would highly appreciate if someone help me get it. Thanks

df1 <-
structure(list(Y = c("1", "2", "3", "4", "5", "6/7", "8-10")),
.Names = "Y", row.names = c(NA, -7L)
, class = "data.frame"
)
library(data.table)
dt1 <- data.table(df1)
dt1[ , c("Y1", "Y2") := tstrsplit(Y, "/", fixed = TRUE)]
dt1
Y Y1 Y2
1: 1 1 NA
2: 2 2 NA
3: 3 3 NA
4: 4 4 NA
5: 5 5 NA
6: 6/7 6 7
7: 8-10 8-10 NA


Required

The required result is

Y Y1
1: 1 1
2: 2 2
3: 3 3
4: 4 4
5: 5 5
6: 6/7 6
7: 8-10 8

Answer

You can just select the first number, i.e.

library(data.table)
dt1[,'Y1' := as.numeric(gsub('[[:punct:]].*', '', Y))]
dt1
#      Y Y1
#1:    1  1
#2:    2  2
#3:    3  3
#4:    4  4
#5:    5  5
#6:  6/7  6
#7: 8-10  8