Home > Mobile >  Combine cells in the first row
Combine cells in the first row

Time:01-25

can somebody help me?

This is my actual data_frame

This is my actual data_frame

and this is what I'd want to get

and this is what I'd want to get

Is there a way to combine only the cells in the first row in R?

I was trying to do it in excel one by one but I've more than 6000 columns so it takes me ages!

Thank you for your help!

CodePudding user response:

You could collapse all the cells in the fist row into one cell and leave the others empty. If x is your data frame:

x[1,] <- c(paste(x[1,], collapse=""), rep("", ncol(x)-1))

CodePudding user response:

you can use the openxlsx package:

library(openxlsx)

> df
# A tibble: 4 x 2
  var1  var2 
  <chr> <chr>
1 A     B   
2 1     2    
3 3     4    
4 5     6  

wb <- createWorkbook()
addWorksheet(wb, "Sheet1")
df[1,] <- paste(df[1,], collapse = "")
writeData(wb, "Sheet1", df)
mergeCells(wb, "Sheet1", cols = (1:2), rows = 2)
saveWorkbook(wb , "E:/test1.xlsx", overwrite = T)

the result would be:

enter image description here

  •  Tags:  
  • Related