Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(data.table)
- library(stringr)
- DT <- data.table(交易筆棟數 = c("土地1建物1車位2", "土地1建物1車位1", "土地1建物1車位0",
- "土地1建物1車位1", "土地1建物1車位1", "土地1建物1車位0"))
- col_names <- str_split(DT$交易筆棟數[1], "\\d+")[[1]][1L:3L]
- get_trade_unit_func <- function(x) {
- temp <- str_match(x, "土地(\\d+)建物(\\d+)車位(\\d+)")
- lapply(2L:4L, function(i) as.numeric(temp[ , i]))
- }
- DT[ , eval(col_names) := get_trade_unit_func(交易筆棟數)]
- DT
- # 交易筆棟數 土地 建物 車位
- # 1: 土地1建物1車位2 1 1 2
- # 2: 土地1建物1車位1 1 1 1
- # 3: 土地1建物1車位0 1 1 0
- # 4: 土地1建物1車位1 1 1 1
- # 5: 土地1建物1車位1 1 1 1
- # 6: 土地1建物1車位0 1 1 0
Advertisement
Add Comment
Please, Sign In to add comment