r语言 - tidyr与多个分离器联合



是否有可能使用tidyr中的unite但修改不同的分离器?

我有:

df1 <- read.table(text="
col1  col2     col3  col4
John  middle   no    Switzerland
Sue   middle   yes   Norway
Nancy low      yes   Germany
Sid   high     no    Germany", header=T)

df<-unite(df1, col1, col2, col3, col4, sep=";", col="var")
head(df)
var
John;middle;no;Switzerland
Sue;middle;yes;Norway
Nanc;low;yes;Germany
Sid;high;no;Germany

这是所需的输出:

head(df)
var
p_John;x_middle;d_no;c_Switzerland
p_Sue;x_middle;d_yes;c_Norway
p_Nanc;x_low;d_yes;c_Germany
p_Sid;x_high;d_no;c_Germany

谢谢。

我不相信您可以,但是创建一个可以执行相同功能的函数很简单:

library(tidyverse)
unite_with_seps <- function(df, name, cols, seps) {
  df <- unite(df, !!name, all_of(cols), sep = "_")
  pattern <- paste(rep("([\w]+)", length(cols)), collapse = "_") # this is a regex which will turn every group of letters surrounded by an underscore into a group 
  replacement <- map_chr(seq_along(seps), ~paste0(seps[.x], "\", .x)) %>% paste0(collapse = ";") # this turns the desired output into a replacement pattern
  mutate(df, !!name := str_replace_all(!!sym(name), pattern, replacement))
}
unite_with_seps(df1, "var", c("col1", "col2", "col3", "col4"), c("p_", "x_", "d_", "c_"))
                                 var
1 p_John;x_middle;d_no;c_Switzerland
2      p_Sue;x_middle;d_yes;c_Norway
3      p_Nancy;x_low;d_yes;c_Germany
4        p_Sid;x_high;d_no;c_Germany

如果完全不清楚,请运行debug(unite_with_seps),然后再次运行该功能,然后逐步通过它,查看变量变为什么。

最新更新