简体   繁体   中英

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 - .

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

How can I do this?

You can just select the first number, ie

library(data.table)
dt1[, Y1 := as.integer(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM