這是tidyr::separate()
泛型的方法。它在 [.data.table
的 j
參數中轉換為 data.table::tstrsplit()
。
用法
# S3 method for dtplyr_step
separate(
data,
col,
into,
sep = "[^[:alnum:]]+",
remove = TRUE,
convert = FALSE,
...
)
參數
- data
-
一個
lazy_dt()
。 - col
-
列名稱或位置。
該參數通過表達式傳遞並支持準引號(您可以取消引用列名稱或列位置)。
- into
-
要創建為字符向量的新變量的名稱。使用
NA
省略輸出中的變量。 - sep
-
列之間的分隔符。默認值是匹配任何非字母數字值序列的正則表達式。
- remove
-
如果為 TRUE,則從輸出 DataFrame 中刪除輸入列。
- convert
-
如果為 TRUE,將在新列上運行 type.convert(),其中 as.is = TRUE。如果組件列是整數、數字或邏輯,這非常有用。
注意:這將導致字符串 "NA"s 轉換為 NA。
- ...
-
傳遞給方法的參數
例子
library(tidyr)
# If you want to split by any non-alphanumeric value (the default):
df <- lazy_dt(data.frame(x = c(NA, "x.y", "x.z", "y.z")), "DT")
df %>% separate(x, c("A", "B"))
#> Source: local data table [4 x 2]
#> Call: copy(DT)[, `:=`(c("A", "B"), tstrsplit(x, split = "[^[:alnum:]]+"))][,
#> `:=`("x", NULL)]
#>
#> A B
#> <chr> <chr>
#> 1 NA NA
#> 2 x y
#> 3 x z
#> 4 y z
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
# If you just want the second variable:
df %>% separate(x, c(NA, "B"))
#> Source: local data table [4 x 1]
#> Call: copy(DT)[, `:=`("B", tstrsplit(x, split = "[^[:alnum:]]+", keep = 2L))][,
#> `:=`("x", NULL)]
#>
#> B
#> <chr>
#> 1 NA
#> 2 y
#> 3 z
#> 4 z
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
# Use regular expressions to separate on multiple characters:
df <- lazy_dt(data.frame(x = c(NA, "x?y", "x.z", "y:z")), "DT")
df %>% separate(x, c("A","B"), sep = "([.?:])")
#> Source: local data table [4 x 2]
#> Call: copy(DT)[, `:=`(c("A", "B"), tstrsplit(x, split = "([.?:])"))][,
#> `:=`("x", NULL)]
#>
#> A B
#> <chr> <chr>
#> 1 NA NA
#> 2 x y
#> 3 x z
#> 4 y z
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
# convert = TRUE detects column classes:
df <- lazy_dt(data.frame(x = c("x:1", "x:2", "y:4", "z", NA)), "DT")
df %>% separate(x, c("key","value"), ":") %>% str
#> List of 12
#> $ parent :List of 12
#> ..$ parent :List of 8
#> .. ..$ parent :Classes ‘data.table’ and 'data.frame': 5 obs. of 1 variable:
#> .. .. ..$ x: chr [1:5] "x:1" "x:2" "y:4" "z" ...
#> .. .. ..- attr(*, ".internal.selfref")=<externalptr>
#> .. ..$ vars : chr "x"
#> .. ..$ groups : chr(0)
#> .. ..$ locals : list()
#> .. ..$ implicit_copy: logi FALSE
#> .. ..$ needs_copy : logi FALSE
#> .. ..$ env :<environment: 0x55df0fded700>
#> .. ..$ name : symbol DT
#> .. ..- attr(*, "class")= chr [1:2] "dtplyr_step_first" "dtplyr_step"
#> ..$ vars : chr [1:3] "x" "key" "value"
#> ..$ groups : chr(0)
#> ..$ locals : list()
#> ..$ implicit_copy : logi TRUE
#> ..$ needs_copy : logi TRUE
#> ..$ env :<environment: 0x55df0fded700>
#> ..$ arrange : NULL
#> ..$ i : NULL
#> ..$ j : language `:=`(c("key", "value"), tstrsplit(x, split = ":"))
#> ..$ on : chr(0)
#> ..$ allow_cartesian: NULL
#> ..- attr(*, "class")= chr [1:2] "dtplyr_step_subset" "dtplyr_step"
#> $ vars : chr [1:2] "key" "value"
#> $ groups : chr(0)
#> $ locals : list()
#> $ implicit_copy : logi TRUE
#> $ needs_copy : logi TRUE
#> $ env :<environment: 0x55df0fded700>
#> $ arrange : NULL
#> $ i : NULL
#> $ j : language `:=`("x", NULL)
#> $ on : chr(0)
#> $ allow_cartesian: NULL
#> - attr(*, "class")= chr [1:2] "dtplyr_step_subset" "dtplyr_step"
df %>% separate(x, c("key","value"), ":", convert = TRUE) %>% str
#> List of 12
#> $ parent :List of 12
#> ..$ parent :List of 8
#> .. ..$ parent :Classes ‘data.table’ and 'data.frame': 5 obs. of 1 variable:
#> .. .. ..$ x: chr [1:5] "x:1" "x:2" "y:4" "z" ...
#> .. .. ..- attr(*, ".internal.selfref")=<externalptr>
#> .. ..$ vars : chr "x"
#> .. ..$ groups : chr(0)
#> .. ..$ locals : list()
#> .. ..$ implicit_copy: logi FALSE
#> .. ..$ needs_copy : logi FALSE
#> .. ..$ env :<environment: 0x55df0fded700>
#> .. ..$ name : symbol DT
#> .. ..- attr(*, "class")= chr [1:2] "dtplyr_step_first" "dtplyr_step"
#> ..$ vars : chr [1:3] "x" "key" "value"
#> ..$ groups : chr(0)
#> ..$ locals : list()
#> ..$ implicit_copy : logi TRUE
#> ..$ needs_copy : logi TRUE
#> ..$ env :<environment: 0x55df0fded700>
#> ..$ arrange : NULL
#> ..$ i : NULL
#> ..$ j : language `:=`(c("key", "value"), tstrsplit(x, split = ":", type.convert = TRUE))
#> ..$ on : chr(0)
#> ..$ allow_cartesian: NULL
#> ..- attr(*, "class")= chr [1:2] "dtplyr_step_subset" "dtplyr_step"
#> $ vars : chr [1:2] "key" "value"
#> $ groups : chr(0)
#> $ locals : list()
#> $ implicit_copy : logi TRUE
#> $ needs_copy : logi TRUE
#> $ env :<environment: 0x55df0fded700>
#> $ arrange : NULL
#> $ i : NULL
#> $ j : language `:=`("x", NULL)
#> $ on : chr(0)
#> $ allow_cartesian: NULL
#> - attr(*, "class")= chr [1:2] "dtplyr_step_subset" "dtplyr_step"
相關用法
- R dtplyr select.dtplyr_step 使用名稱對列進行子集化
- R dtplyr slice.dtplyr_step 使用行的位置對行進行子集化
- R dtplyr summarise.dtplyr_step 將每組匯總為一行
- R dtplyr lazy_dt 創建一個“惰性”data.table 以與 dplyr 動詞一起使用
- R dtplyr group_modify.dtplyr_step 對每個組應用一個函數
- R dtplyr transmute.dtplyr_step 創建新列,刪除舊列
- R dtplyr left_join.dtplyr_step 連接數據表
- R dtplyr fill.dtplyr_step 用上一個或下一個值填充缺失值
- R dtplyr filter.dtplyr_step 使用列值對行進行子集化
- R dtplyr mutate.dtplyr_step 創建和修改列
- R dtplyr distinct.dtplyr_step 子集不同/唯一行
- R dtplyr unite.dtplyr_step 通過將字符串粘貼在一起將多列合並為一列。
- R dtplyr nest.dtplyr_step 巢
- R dtplyr relocate.dtplyr_step 使用變量名稱重新定位變量
- R dtplyr head.dtplyr_step 對第一行或最後一行進行子集化
- R dtplyr expand.dtplyr_step 擴展 DataFrame 以包含所有可能的值組合。
- R dtplyr group_by.dtplyr_step 分組和取消分組
- R dtplyr intersect.dtplyr_step 設置操作
- R dtplyr pivot_wider.dtplyr_step 將數據從長軸轉向寬軸
- R dtplyr count.dtplyr_step 按組計數觀察值
- R dtplyr drop_na.dtplyr_step 刪除包含缺失值的行
- R dtplyr complete.dtplyr_step 完成缺少數據組合的 DataFrame
- R dtplyr collect.dtplyr_step 強製計算惰性 data.table
- R dtplyr arrange.dtplyr_step 按列值排列行
- R dtplyr rename.dtplyr_step 使用名稱重命名列
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Separate a character column into multiple columns with a regular expression or numeric locations。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。