简体   繁体   English

合并具有相似列名/相似列字符串的列 - Reprex

[英]Combine Columns with Similar Column Names / Similar Column Strings - Reprex

Goals: To merge multiple columns just based on the similarity of the column name.目标:仅根据列名的相似性合并多个列。

Issues: I am dealing with a large data set where the column names are replicated and look like this: wk1.1, wk1.2, wk1.3.问题:我正在处理一个大型数据集,其中列名被复制,看起来像这样:wk1.1、wk1.2、wk1.3。 For each row, there will be only one value in the similar column names, and the others will be NA.对于每一行,相似的列名中只有一个值,其他为 NA。 Coalesce is very helpful, but becomes tedious (messes up automation) when I have to list each column name. Coalesce 非常有用,但是当我必须列出每个列名时变得乏味(搞乱自动化)。 Is there a way to coalesce based off a string of characters?有没有办法根据一串字符进行合并? For instance below, I would prefer to coalesce %in% "wk1."例如下面,我希望合并 %in% "wk1"。

library(dplyr)
wk1.1 <- c(15, 4, 1)
wk1.2 <- c(3, 20, 4)
wk1.3 <- c(1, 2, 17)

df <- data.frame(wk1.1, wk1.2, wk1.3)
df[df < 14] <- NA
df1 <- df %>%
  mutate(wk1 = coalesce(df$wk1.1, df$wk1.2, df$wk1.3))

We can use splice it with !!!我们可以使用拼接它!!!

library(dplyr)
df %>%
      mutate(wk1 = coalesce(!!! .))
#  wk1.1 wk1.2 wk1.3 wk1
#1    15    NA    NA  15
#2    NA    20    NA  20
#3    NA    NA    17  17

Or another option is to reduce and apply coalesce或者另一种选择是reduce并应用coalesce

library(purrr)
df %>%
   mutate(wk1 = reduce(., coalesce))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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