How write excel with xlsx and set specific columns and row order?

Hi community

Im want create a excel file with 15 columns and 50 rows for order the data.

I create a .xlsx file but put all rows in a single column.

library(openxlsx)
Accesiones2 <- data.frame(ACC=c("'G   24',", "'G  114',", "'G  151',", "'G  193',", "'G  218',", 
                            "'G  265',", "'G  285',", "'G  345',", "'G  372',", "'G  386',", 
                            "'G  400',", "'G  417',", "'G  424',", "'G  437',", "'G  475',", 
                            "'G  512',", "'G  525',", "'G  547',", "'G  548',", "'G  777',", 
                            "'G  826',", "'G  885',", "'G  896',", "'G  913',", "'G 1051',", 
                            "'G 1076',", "'G 1113',", "'G 1267',", "'G 1319',", "'G 1463',", 
                            "'G 1531',", "'G 1556',", "'G 1557',", "'G 1633',", "'G 1948',", 
                            "'G 2431',", "'G 2577',", "'G 2993',", "'G 3012',", "'G 3222',", 
                            "'G 3573',", "'G 4028',", "'G 4331',", "'G 4510',", "'G 4704',", 
                            "'G 4715',", "'G 4757',", "'G 5074',", "'G 5279',", "'G 5702',")) # example data

write.xlsx(Accesiones2,"D:\\user\\Documents\\test.xlsx",colNames = FALSE, rowNames = FALSE)

Tnks!

Your code only defines one column.

Accesiones2 <- data.frame(ACC=c("'G   24',", "'G  114',", "'G  151',", "'G  193',", "'G  218',", 
                                 "'G  265',", "'G  285',", "'G  345',", "'G  372',", "'G  386',", 
                                 "'G  400',", "'G  417',", "'G  424',", "'G  437',", "'G  475',", 
                                 "'G  512',", "'G  525',", "'G  547',", "'G  548',", "'G  777',", 
                                 "'G  826',", "'G  885',", "'G  896',", "'G  913',", "'G 1051',", 
                                 "'G 1076',", "'G 1113',", "'G 1267',", "'G 1319',", "'G 1463',", 
                                 "'G 1531',", "'G 1556',", "'G 1557',", "'G 1633',", "'G 1948',", 
                                 "'G 2431',", "'G 2577',", "'G 2993',", "'G 3012',", "'G 3222',", 
                                 "'G 3573',", "'G 4028',", "'G 4331',", "'G 4510',", "'G 4704',", 
                                 "'G 4715',", "'G 4757',", "'G 5074',", "'G 5279',", "'G 5702',")) # example data
str(Accesiones2)
'data.frame':	50 obs. of  1 variable:
 $ ACC: chr  "'G   24'," "'G  114'," "'G  151'," "'G  193'," ...

How do you want the data distributed among the columns?

Yes, the data frame have only 1 columns with 753 rows. But I need make an excel and redistribute in 15 columns and 50 rows, for better see the data and use for paste in a query in Oracle.

If you wanted to reshape the data you posted into a table with 10 rows of 5 columns, you could use the following code.

library(openxlsx)
library(dplyr)
library(tidyr)

Accesiones2 <- data.frame(ACC=c("'G   24',", "'G  114',", "'G  151',", "'G  193',", "'G  218',", 
                                "'G  265',", "'G  285',", "'G  345',", "'G  372',", "'G  386',", 
                                "'G  400',", "'G  417',", "'G  424',", "'G  437',", "'G  475',", 
                                "'G  512',", "'G  525',", "'G  547',", "'G  548',", "'G  777',", 
                                "'G  826',", "'G  885',", "'G  896',", "'G  913',", "'G 1051',", 
                                "'G 1076',", "'G 1113',", "'G 1267',", "'G 1319',", "'G 1463',", 
                                "'G 1531',", "'G 1556',", "'G 1557',", "'G 1633',", "'G 1948',", 
                                "'G 2431',", "'G 2577',", "'G 2993',", "'G 3012',", "'G 3222',", 
                                "'G 3573',", "'G 4028',", "'G 4331',", "'G 4510',", "'G 4704',", 
                                "'G 4715',", "'G 4757',", "'G 5074',", "'G 5279',", "'G 5702',")) # example data


Accesiones2 <- Accesiones2 |> mutate(Col_Label = paste0("C", rep(1:5, 10)),
                                     Row_label = rep(1:10, each = 5))
WideDF <- pivot_wider(Accesiones2, names_from = "Col_Label", 
                      values_from = "ACC") |> 
  select(-Row_label)

WideDF
#> # A tibble: 10 × 5
#>    C1        C2        C3        C4        C5       
#>    <chr>     <chr>     <chr>     <chr>     <chr>    
#>  1 'G   24', 'G  114', 'G  151', 'G  193', 'G  218',
#>  2 'G  265', 'G  285', 'G  345', 'G  372', 'G  386',
#>  3 'G  400', 'G  417', 'G  424', 'G  437', 'G  475',
#>  4 'G  512', 'G  525', 'G  547', 'G  548', 'G  777',
#>  5 'G  826', 'G  885', 'G  896', 'G  913', 'G 1051',
#>  6 'G 1076', 'G 1113', 'G 1267', 'G 1319', 'G 1463',
#>  7 'G 1531', 'G 1556', 'G 1557', 'G 1633', 'G 1948',
#>  8 'G 2431', 'G 2577', 'G 2993', 'G 3012', 'G 3222',
#>  9 'G 3573', 'G 4028', 'G 4331', 'G 4510', 'G 4704',
#> 10 'G 4715', 'G 4757', 'G 5074', 'G 5279', 'G 5702',

Created on 2023-01-10 with reprex v2.0.2
Can you use a version of that to reshape your full data set? Your write.xlsx() should then give you the excel file you want.

1 Like

Was the solution. Im adapted about my data.
Tnks!

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.