Import of Excel sheet in R and handle errrors with date

29 Views Asked by At

I have a problem with the recognition of dates while importing data from Excel to R. I want to import the following test sheet from Excel to R:

Excel Sheet

My R code is:

library(readxl)
excel_datei <- "C:/Users/aklip/Desktop/Masterarbeit/Datenbearbeitung Test/Daten/Liquidität 2014.xlsx"
daten <- read_excel(excel_datei)

The data import is working, but the dates are not recognized as dates but as header and num.

Dates in R_1:

daten in R_1

Dates in R_2:

daten in R_2

As I want to analyze the data as a time series, it is necessary that R recognizes that there is a value per each MFI-ID and for every date in the dataset.

Therefor I wanted to transfer these data into a long table:

# Umwandlung der Daten in das gewünschte Format
daten_long <- pivot_longer(daten, cols = -`MFI-ID`, names_to = "Datum", values_to = "Liq")

# Spalte "Datum" in das gewünschte Datumsformat konvertieren (falls es nicht bereits im richtigen Format ist)
daten_long$Datum <- as.Date(str, "%d.%m.%Y")

# Die umgewandelten Daten ausgeben
print(daten_long)


MFI-ID` Datum    Liq
   <chr>    <date> <dbl>
 1 DE000011 NA       100
 2 DE000011 NA       110
 3 DE000011 NA       120
 4 DE000011 NA       130
 5 DE000011 NA       140
 6 DE000012 NA       120
 7 DE000012 NA       130
 8 DE000012 NA       140
 9 DE000012 NA       150
10 DE000012 NA       160
11 DE000013 NA       130
12 DE000013 NA       140
13 DE000013 NA       150
14 DE000013 NA       160
15 DE000013 NA       170
16 DE000014 NA       140
17 DE000014 NA       150
18 DE000014 NA       160
19 DE000014 NA       170
20 DE000014 NA       180

Unfortunately the date is not recognized as date. Maybe you have a good idea how to handle this problem. Maybe there are better ways to do this.

0

There are 0 best solutions below