Convert data frame rows to a vector (each element is separated by space)

Suppose I have the following data frame.

df
#> # A tibble: 4 x 1
#>   x    
#>   <chr>
#> 1 abc  
#> 2 def  
#> 3 ijk  
#> 4 pqr

How can I combine all the rows of column x separated by space and eventually get them as a vector?

vector_wanted 
#> [1] "abc def ijk pqr"
# Toy data
library(dplyr)
df <- tibble(x = c(
  "abc", "def", "ijk", "pqr"
))

Here is one way.

df <- tibble(x = c(
   "abc", "def", "ijk", "pqr"
 ))
df
# A tibble: 4 x 1
  x    
  <chr>
1 abc  
2 def  
3 ijk  
4 pqr  
paste(df$x, collapse = " ")
[1] "abc def ijk pqr"
1 Like

@FJCC many thanks for the solution!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.