Stack Exchange
combine 4 columns to make it one column in R
#dput(df)
structure(list(product = c(1L, 1L, 2L, 2L, 2L, 3L, 3L, 4L, 4L
), q1 = c(5L, 1L, 2L, 3L, 4L, 5L, 1L, 1L, 2L), q2 = c(9L, 7L, 
4L, 6L, 4L, 7L, 2L, 2L, 4L), q3 = c(NA, NA, 2L, 3L, NA, 5L, 4L, 
NA, NA)), class = "data.frame", row.names = c(NA, -9L))





q1
q1
q1
q1
q2
q2
q2
q2




1
2
3
4
1
2
3
4


8
9
5
6
7
8
8
8


5
6
6
5
4
3
2
5


3
2
3
6
6
5
5
6




To
  q1      q2
1 2 3 4  1 2 3 4
8 9 5 6  7 8 8 8
5 6 6 5  4 3 2 5

I wanted to combine some 4 column headers to one big header. If possible i would like to leave a blank column after the q1. Thanks in advance
Also, am open to suggestions for a good package for table formatting in R

Can you share a reproducible example and your code? The table above is probably not a real data frame, since you can‘t have repeated column names. Also combining the values into one column is only possible if you convert them to character (check the str_c function from stringr package). Maybe you can describe your problem and what you really want to achieve. I‘m pretty sure there‘s a more standard solution than concatenating values and put an emtpy column in between.

Hi Deschen, I have posted a reproducible example. Yes, i have noticed that just now all the column names changed, as i imported the dataset.  My main objective is to make all relative column headers into one column header. As, when i am calling the header for another code that table should come. I am not sure if it is possible, but if it is i would like to try.

Assuming your data is in a csv, you could try something like this:
#wherever you store the csv file
df = read.csv("../example.csv")

#since you are not allowed duplicate column names in a dataframe, read.csv will correct that by adding ".1",, ".2" etc to duplicates.
> df
  q1 q1.1 q1.2 q1.3 q2 q2.1 q2.2 q2.3
1  1    2    3    4  1    2    3    4
2  8    9    5    6  7    8    8    8
3  5    6    6    5  4    3    2    5
4  3    2    3    6  6    5    5    6
> 
> library(tidyr) 
> df %>%
+   unite("q1",colnames(df)[grepl("^q1",colnames(df))],sep = " ") %>%
+   unite("q2",colnames(df)[grepl("^q2",colnames(df))],sep = " ") 
       q1      q2
1 1 2 3 4 1 2 3 4
2 8 9 5 6 7 8 8 8
3 5 6 6 5 4 3 2 5
4 3 2 3 6 6 5 5 6
> 


Here is an option with pivoting to 'long' format
library(dplyr)
library(tidyr)
library(stringr)
df %>%
    mutate(rn = row_number()) %>%
    pivot_longer(cols = -rn, names_to = c(".value", 'grp'), names_sep="\\.") %>%
    group_by(rn) %>% 
    summarise(across(c(q1, q2), str_c, collapse=" "), .groups = 'drop') %>% 
    select(-rn)
# A tibble: 4 x 2
#  q1      q2     
#  <chr>   <chr>  
#1 1 2 3 4 1 2 3 4
#2 8 9 5 6 7 8 8 8
#3 5 6 6 5 4 3 2 5
#4 3 2 3 6 6 5 5 6

data
df <- structure(list(q1.1 = c(1L, 8L, 5L, 3L), q1.2 = c(2L, 9L, 6L, 
2L), q1.3 = c(3L, 5L, 6L, 3L), q1.4 = c(4L, 6L, 5L, 6L), q2.1 = c(1L, 
7L, 4L, 6L), q2.2 = c(2L, 8L, 3L, 5L), q2.3 = c(3L, 8L, 2L, 5L
), q2.4 = c(4L, 8L, 5L, 6L)), class = "data.frame", row.names = c("1", 
"2", "3", "4"))

