1

I have an Excel spreadsheet called "my_data". I can recreate the spreadsheet in R as follows:

library(tibble); library(readxl) tibble(x = c(1L, 2L, 3L), y = c("A", "B", "C")) -> my_data 

On import, I can specify column types like this:

 read_excel("/Desktop/my_data.xlsx", col_types = c("numeric", "text") -> my_data 

But the vector supplied to col_types needs to be in the same order as my spreadsheet to give correct results. If I move the order of columns in the spreadsheet or add/delete columns, col_types will be wrong.

Can I supply the column types for specific columns and leave read_excel to guess the types for the rest of the columns?

2
  • I hope there's an elegant way to do this. One kludgy way would be to load once, scan the headers and join that to a table specifying what type "ColNameA" should be etc, and what an unknown default should be, and then load again with those specifications. Commented Jan 15 at 19:26
  • What do you know about your specifics colums? The column name? type_convert() can be use to reconvert data-type in readr:: style. So you could read_excel(), relocate() the columns you know about, and type_convert("numeric", "guess", "guess. ...) Take a look here: stackoverflow.com/questions/64430198/… Commented Jan 15 at 19:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.