2

I have about 20 columns in an excel file. I am using read_excel to read them into R as a dataframe. One column is being taken as Posix and I want it to be text.

I know that mentioning the types of all the column types would solve this but I wanted to see if I could mention the columns' name or something to specify that only that column should be taken as text

So if i have a columns in the excel as Name, Age, DOB, Gender, time of birth I want to implement something like this

df <- read_excel("excel.xlsx", col_types = 'time of birth':"text")
instead of
df <- read.excel("excel.xlsx", col_types = c("text","text","date","text","text"))

Thanks!

2 Answers2

0

I don't believe you can. The documentation for col_types says:

Either NULL to guess all from the spreadsheet or a character vector containing one entry per column from these options: "skip", "guess", "logical", "numeric", "date", "text" or "list". If exactly one col_type is specified, it will be recycled.

svenhalvorson
  • 1,090
  • 8
  • 21
0

You can pass a named vector as the argument for col_types =

library(readxl)

instead of:
df <- read_excel("excel.xlsx", col_types = c("text","text","date","text","text"))
try:
df <- read_excel("excel.xlsx", col_types = c("time of birth" = "text"))