简体   繁体   中英

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

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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