Home > OS >  Excel date format not imported correctly into R
Excel date format not imported correctly into R

Time:04-11

I am importing an Excel file into R, where the date format in Excel is "27-02-2012". However, once I import the dataset into R with the code below:

#Loading packages
library(tidyverse)
library(readxl)
library(writexl)
library(stringr)
library(textclean)
library(lubridate)
library(zoo)

import data

data_corpus <- read_excel("data.xlsx",
                                   sheet= "xyz")

The date format in some rows stays as "27-02-2012", while other rows look as follows "40911".

Is it possible to convert all values under the "date" column to have the following format: "27-02-2012"?

Here is a data exmaple:

sapply(data_corpus, class)

output:

   post                      date                  
  "character"               "character" 

I have tried the following code, but it turns all values in "date" into NAs:

data_corpus$date <- as_date(data_corpus$date)

Sample:

data_corpus$post[2]
[1] this is really unfortunateا"
> data_corpus$date[2]
[1] "27-02-2012"

CodePudding user response:

Try using the col_types parameter

data_corpus <- read_excel("data.xlsx", sheet= "xyz", col_types = c("text", "date"))
  • Related