Importing excel file with read_excel function: Date columns is not correctly imported
up vote
1
down vote
favorite
I did the following code to import a excel file in Rstudio:
(nms <- names(read_excel("myexcelfile.xlsx")))
(ct <- ifelse(grepl("^Date", nms), "text", "numeric"))
read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]
The resul is this dataframe below:
structure(list(Data = c("41731", "41730", "41729", "41726", "41725"
), ABEV3 = c(15.2, 14.9, 15.22, 15.15, 15.18)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
The first column should be the dates (Brazilian Format: Day/Month/Year).

How can I fix this?
r tidyverse
add a comment |
up vote
1
down vote
favorite
I did the following code to import a excel file in Rstudio:
(nms <- names(read_excel("myexcelfile.xlsx")))
(ct <- ifelse(grepl("^Date", nms), "text", "numeric"))
read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]
The resul is this dataframe below:
structure(list(Data = c("41731", "41730", "41729", "41726", "41725"
), ABEV3 = c(15.2, 14.9, 15.22, 15.15, 15.18)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
The first column should be the dates (Brazilian Format: Day/Month/Year).

How can I fix this?
r tidyverse
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I did the following code to import a excel file in Rstudio:
(nms <- names(read_excel("myexcelfile.xlsx")))
(ct <- ifelse(grepl("^Date", nms), "text", "numeric"))
read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]
The resul is this dataframe below:
structure(list(Data = c("41731", "41730", "41729", "41726", "41725"
), ABEV3 = c(15.2, 14.9, 15.22, 15.15, 15.18)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
The first column should be the dates (Brazilian Format: Day/Month/Year).

How can I fix this?
r tidyverse
I did the following code to import a excel file in Rstudio:
(nms <- names(read_excel("myexcelfile.xlsx")))
(ct <- ifelse(grepl("^Date", nms), "text", "numeric"))
read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]
The resul is this dataframe below:
structure(list(Data = c("41731", "41730", "41729", "41726", "41725"
), ABEV3 = c(15.2, 14.9, 15.22, 15.15, 15.18)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
The first column should be the dates (Brazilian Format: Day/Month/Year).

How can I fix this?
r tidyverse
r tidyverse
asked Nov 9 at 22:41
Diogo Bastos
434312
434312
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Try this:
(nms <- names(read_excel("myexcelfile.xlsx")))
(ct <- ifelse(grepl("^Date", nms), "date", "numeric"))
df <- read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]
df$Date <- format(as.Date(as.character(df$Date)), "%d-%m-%Y")
Instead of importing as text, import it as date and then format it in the way described below (there I suppose that you will save the Excel in the df dataframe and that the column will be called Date, but adjust as needed).
It doesnt work: I have this message:Error in charToDate(x) : string de caracteres não é um formato padrão não ambíguo
– Diogo Bastos
Nov 9 at 23:00
I set arbitrarily:as.Date(df$Date,origin = "1899-12-30")and it works. Why?
– Diogo Bastos
Nov 9 at 23:25
This is the origin used by Excel. However I'm not sure why the approach above didn't work straight away, perhaps this is linked to different time zones.
– arg0naut
Nov 9 at 23:35
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Try this:
(nms <- names(read_excel("myexcelfile.xlsx")))
(ct <- ifelse(grepl("^Date", nms), "date", "numeric"))
df <- read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]
df$Date <- format(as.Date(as.character(df$Date)), "%d-%m-%Y")
Instead of importing as text, import it as date and then format it in the way described below (there I suppose that you will save the Excel in the df dataframe and that the column will be called Date, but adjust as needed).
It doesnt work: I have this message:Error in charToDate(x) : string de caracteres não é um formato padrão não ambíguo
– Diogo Bastos
Nov 9 at 23:00
I set arbitrarily:as.Date(df$Date,origin = "1899-12-30")and it works. Why?
– Diogo Bastos
Nov 9 at 23:25
This is the origin used by Excel. However I'm not sure why the approach above didn't work straight away, perhaps this is linked to different time zones.
– arg0naut
Nov 9 at 23:35
add a comment |
up vote
1
down vote
Try this:
(nms <- names(read_excel("myexcelfile.xlsx")))
(ct <- ifelse(grepl("^Date", nms), "date", "numeric"))
df <- read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]
df$Date <- format(as.Date(as.character(df$Date)), "%d-%m-%Y")
Instead of importing as text, import it as date and then format it in the way described below (there I suppose that you will save the Excel in the df dataframe and that the column will be called Date, but adjust as needed).
It doesnt work: I have this message:Error in charToDate(x) : string de caracteres não é um formato padrão não ambíguo
– Diogo Bastos
Nov 9 at 23:00
I set arbitrarily:as.Date(df$Date,origin = "1899-12-30")and it works. Why?
– Diogo Bastos
Nov 9 at 23:25
This is the origin used by Excel. However I'm not sure why the approach above didn't work straight away, perhaps this is linked to different time zones.
– arg0naut
Nov 9 at 23:35
add a comment |
up vote
1
down vote
up vote
1
down vote
Try this:
(nms <- names(read_excel("myexcelfile.xlsx")))
(ct <- ifelse(grepl("^Date", nms), "date", "numeric"))
df <- read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]
df$Date <- format(as.Date(as.character(df$Date)), "%d-%m-%Y")
Instead of importing as text, import it as date and then format it in the way described below (there I suppose that you will save the Excel in the df dataframe and that the column will be called Date, but adjust as needed).
Try this:
(nms <- names(read_excel("myexcelfile.xlsx")))
(ct <- ifelse(grepl("^Date", nms), "date", "numeric"))
df <- read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]
df$Date <- format(as.Date(as.character(df$Date)), "%d-%m-%Y")
Instead of importing as text, import it as date and then format it in the way described below (there I suppose that you will save the Excel in the df dataframe and that the column will be called Date, but adjust as needed).
edited Nov 9 at 23:33
answered Nov 9 at 22:53
arg0naut
1,592312
1,592312
It doesnt work: I have this message:Error in charToDate(x) : string de caracteres não é um formato padrão não ambíguo
– Diogo Bastos
Nov 9 at 23:00
I set arbitrarily:as.Date(df$Date,origin = "1899-12-30")and it works. Why?
– Diogo Bastos
Nov 9 at 23:25
This is the origin used by Excel. However I'm not sure why the approach above didn't work straight away, perhaps this is linked to different time zones.
– arg0naut
Nov 9 at 23:35
add a comment |
It doesnt work: I have this message:Error in charToDate(x) : string de caracteres não é um formato padrão não ambíguo
– Diogo Bastos
Nov 9 at 23:00
I set arbitrarily:as.Date(df$Date,origin = "1899-12-30")and it works. Why?
– Diogo Bastos
Nov 9 at 23:25
This is the origin used by Excel. However I'm not sure why the approach above didn't work straight away, perhaps this is linked to different time zones.
– arg0naut
Nov 9 at 23:35
It doesnt work: I have this message:
Error in charToDate(x) : string de caracteres não é um formato padrão não ambíguo– Diogo Bastos
Nov 9 at 23:00
It doesnt work: I have this message:
Error in charToDate(x) : string de caracteres não é um formato padrão não ambíguo– Diogo Bastos
Nov 9 at 23:00
I set arbitrarily:
as.Date(df$Date,origin = "1899-12-30") and it works. Why?– Diogo Bastos
Nov 9 at 23:25
I set arbitrarily:
as.Date(df$Date,origin = "1899-12-30") and it works. Why?– Diogo Bastos
Nov 9 at 23:25
This is the origin used by Excel. However I'm not sure why the approach above didn't work straight away, perhaps this is linked to different time zones.
– arg0naut
Nov 9 at 23:35
This is the origin used by Excel. However I'm not sure why the approach above didn't work straight away, perhaps this is linked to different time zones.
– arg0naut
Nov 9 at 23:35
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234183%2fimporting-excel-file-with-read-excel-function-date-columns-is-not-correctly-imp%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown