Use Dplyr to Concatenate a Column

Concatenating two text columns in dplyr

You can use the unite function from tidyr

require(tidyverse)

df %>%
unite(round_experiment, c("round", "experiment"))

round_experiment results
1 A_V1 8.797624
2 A_V2 9.721078
3 A_V3 10.519000
4 B_V1 9.714066
5 B_V2 9.952211
6 B_V3 9.642900

dplyr mutate in R - add column as concat of columns

You need to use sep = not collapse =, and why use sort?. And I used paste and not paste0.

library(dplyr)
states.df <- data.frame(name = as.character(state.name),
region = as.character(state.region),
division = as.character(state.division))
res = mutate(states.df,
concated_column = paste(name, region, division, sep = '_'))

As far as the sorting goes, you do not use sort correctly. Maybe you want:

as.data.frame(lapply(states.df, sort))

This sorts each column, and creates a new data.frame with those columns.

dplyr concatenate column by variable value

Here are some other options -

  1. Use .data -
library(dplyr)

a <- "x"
df %>% group_by(y) %>% summarise(z = toString(.data[[a]]))

# y z
# <chr> <chr>
#1 A 1, 3
#2 B 2

  1. get
df %>% group_by(y) %>% summarise(z = toString(get(a)))

  1. as.name
df %>% group_by(y) %>% summarise(z = toString(!!as.name(a)))

paste(..., collapse = ',') is equivalent to toString.

use dplyr to combine columns of data.frame when column names are not known

With a little trial and error:

colNames_as_symbols <- syms(names(myTibble))
transmute(myTibble, concat = paste(!!!colNames_as_symbols, sep = '.'))

Here was the hint that put me on to the solution... From the documentation for !!!:

The big-bang operator !!! forces-splice a list of objects. The
elements of the list are spliced in place, meaning that they each
become one single argument.

vars <- syms(c("height", "mass"))

Force-splicing is equivalent to supplying the elements separately:

starwars %>% select(!!!vars)
starwars %>% select(height, mass)

In fact, the entire documentation entitled "Force parts of an expression" is fascinating reading. It can be accessed by issuing ?qq_show

use dplyr to concatenate a column

You need to collapse the values in paste

df %>% group_by(id) %>% summarise(vector=paste(A, collapse=" "))

R: Concatenate strings from one column based on repeating strings in another column

The tricky part is that you need to group by chunks of x/y/z. Below is one approach. Once you have your id to group by you can simply summarize and concatenate the strings.

library(tidyverse)

df <- data.frame(element = c(letters[24:26], 'z', letters[24:26]),
string = c('abc', 'def', 'ghi', 'ijk', 'lmn', 'o', 'p'))

df %>%
mutate(id = cumsum(if_else(element < lag(element), 1, 0, missing = 1))) %>%
group_by(id) %>%
summarize(strong = str_c(string, collapse = ' '), .groups = 'drop')

With the above test data, this gives:

# A tibble: 2 x 2
id strong
<dbl> <chr>
1 1 abc def ghi ijk
2 2 lmn o p

Concat a list column in R

Try this. Keep in mind that the element in your tibble is a list. So you can use any of these approaches:

library(tidyverse)
tibble(b=list(letters[1:3])) %>%
mutate(b = lapply(b,function(x)paste0(x,collapse = '')))

Or this:

#Code 2
tibble(b=list(letters[1:3])) %>%
mutate(b = sapply(b,function(x)paste0(x,collapse = '')))

Output:

# A tibble: 1 x 1
b
<chr>
1 abc

In the first case, you will get the result in a list whereas in the second one you will get it as a value.

Concatenating strings / rows using dplyr, group_by & collapse or summarize, but maintain NA values

We may use str_c from the stringr package.

library(dplyr)
library(stringr)

df %>%
group_by(ID)%>%
summarize(string = str_c(string, collapse = "; "))

# ID string
# <dbl> <chr>
#1 1 " asfdas ; sdf"
#2 2 NA
#3 3 "NA"

dplyr concat columns stored in variable (mutate and non standard evaluation)

You can try syms from rlang:

library(dplyr)
packageVersion('dplyr')
#[1] ‘0.7.0’
df <- dplyr::data_frame(a = letters[1:3], b = letters[4:6], c = letters[7:9])
cols_to_concat = c("a", "b", "c")

library(rlang)
cols_quo <- syms(cols_to_concat)
df %>% mutate(concat = paste0(!!!cols_quo))

# or
df %>% mutate(concat = paste0(!!!syms(cols_to_concat)))

# # A tibble: 3 x 4
# a b c concat
# <chr> <chr> <chr> <chr>
# 1 a d g adg
# 2 b e h beh
# 3 c f i cfi

Concatenate string field using dplyr when some values are NA

You can use na.omit to drop the NA values, na_if would change the empty values to NA.

library(dplyr)

input %>%
group_by(Location) %>%
summarise(Comment = na_if(paste0(na.omit(Comment), collapse = '|'), ''))

# Location Comment
# <int> <chr>
#1 1 This is a comment|This is another comment
#2 2 This is a comment
#3 3 This is a comment
#4 4 NA


Related Topics



Leave a reply



Submit