How to mutate columns whose column names differ by suffix?

In a dataset, for example

data_frame(a=letters, a_1=letters, b=letters, b_1=letters)

I would like to combine columns with a similar "root", namely ac a_1and bc b_1. The result should look like

# A tibble: 26 x 2
       a     b
   <chr> <chr>
 1   a a   a a
 2   b b   b b
 3   c c   c c
 4   d d   d d
 5   e e   e e
 6   f f   f f
 7   g g   g g
 8   h h   h h
 9   i i   i i
10   j j   j j
# ... with 16 more rows
+4
source share
2 answers

If you are looking for a tidy approach, you can do this using tidyr::unite_:

library(tidyr)

# get a list column name groups
cols <- split(names(df), sub("_.*", "", names(df)))

# loop through list and unite columns
for(x in names(cols)) {
  df <- unite_(df, x, cols[[x]], sep = " ")
}
+4
source

Here is one way to do this,

ind <- sub('_.*', '', names(df))
as.data.frame(sapply(unique(ind), function(i) do.call(paste, df[i == ind])))
#     a   b
#1  a a a a
#2  b b b b
#3  c c c c
#4  d d d d
#5  e e e e
#6  f f f f
#7  g g g g
#8  h h h h
+1
source

Source: https://habr.com/ru/post/1681437/


All Articles