1

I am importing data from multiple sheet Excel workbook using rio package in R. The code is super simple below:

library(rio)
my <- import_list("test.xls")

This is a list of data-frames. The problem is that the first row automatically becomes a header while I do not have any headers and it's just a data. In the description of package I didn't find the way to read worksheet with

header = FLASE

So, how can I convert this header to data row?

Mary
  • 165
  • 11
  • Duplicate question. colnames(DF) <- as.character(unlist(DF[1,])) DF = DF[-1, ] According to https://stackoverflow.com/questions/20956119/assign-headers-based-on-existing-row-in-dataframe-in-r – TJ83 Apr 30 '19 at 20:12
  • 1
    Actually, it seems like the opposite of that. – divibisan Apr 30 '19 at 20:26

1 Answers1

2

Assuming you can't import your data properly using that function (and I strongly recommend that you read the documentation for that function throughly, as the argument you're looking for is very likely to exist - it likely just has a different name than in read.table) you can access the "header" using colnames, then just rbind it on top of your data:

df2 <- rbind(colnames(mtcars), mtcars)
head(df2)

                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
1                  mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4           21   6  160 110  3.9  2.62 16.46  0  1    4    4
Mazda RX4 Wag       21   6  160 110  3.9 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85  2.32 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15  3.44 17.02  0  0    3    2

Then you can assign new column names with colnames(df2) <- ...:

# Assign numbers as column names
colnames(df2) <- paste0('V', seq_len(ncol(df2)))
head(df2)

                    V1  V2   V3  V4   V5    V6    V7 V8 V9  V10  V11
1                  mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4           21   6  160 110  3.9  2.62 16.46  0  1    4    4
Mazda RX4 Wag       21   6  160 110  3.9 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85  2.32 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15  3.44 17.02  0  0    3    2
divibisan
  • 11,659
  • 11
  • 40
  • 58
  • thank you! have you ever heard about rio-package? do you know if it's possible just to load the data correctly? – Mary Apr 30 '19 at 21:33
  • No, but you can find the documentation online, or by entering `? import_list` in the R console – divibisan Apr 30 '19 at 21:36
  • yeah, I read their documention, and didn't find anything. Just thought that I might miss something because it should be basic step. – Mary Apr 30 '19 at 22:57
  • 1
    It seems like `import_list` calls `import` which uses `readxl::read_excel` to load excel files. So any you'd want to look there for the appropriate arguments to use. It seems like `col_names = TRUE` is the default so it should be working, though. – divibisan May 01 '19 at 15:11