I am trying to read dates from different excel files and each of them have the dates stored in different formats (character or date). This is making the date column on each file being read as character "28/02/2020" or as the numeric conversion excel does to the dates "452344" (number of days since 1900)
files1 = list.files(pattern="*.xlsx")
df = lapply(files1, read_excel,col_types = "text")
df = do.call(rbind, df)
¿How can I make R to read the character type "28/02/2020" and not the "452344" numeric type?
For multiple date format in one column I suggest using lubridate::parse_date_time() (or any other date converter that converts ambiguous format to NA instead of printing an error message)
I assume your df should look something like this:
# A tibble: 6 x 2
id date
<chr> <chr>
1 1 43889
2 2 43889
3 3 43889
4 1 28/02/2020
5 2 28/02/2020
6 3 28/02/2020
Then you should use this code:
library(lubridate)
df <- as.data.frame(df)
df$date2 <- parse_date_time(x = df$date, orders = "d m y") #converts rows like "28/02/2020" to date
df[is.na(df$date2),"date2"] <- as.Date(as.numeric(df[is.na(df$date2),"date"]), origin = "1899-12-30") #converts rows like "43889"
R output:
id date date2
1 1 43889 2020-02-28
2 2 43889 2020-02-28
3 3 43889 2020-02-28
4 1 28/02/2020 2020-02-28
5 2 28/02/2020 2020-02-28
6 3 28/02/2020 2020-02-28
str(df)
'data.frame': 6 obs. of 3 variables:
$ id : chr "1" "2" "3" "1" ...
$ date : chr "43889" "43889" "43889" "28/02/2020" ...
$ date2: POSIXct, format: "2020-02-28" "2020-02-28" "2020-02-28" "2020-02-28" ...
I know it is not the nicest solution but it should work for you as well
Related
I would like to convert my Date column into real date format. I cannot convert with as.Date. Could you please help me? I tried with following. But it doesn't work.
1)as.POSIXct(as.character(df1$Date, format="%Y%m%d"))
2)as.Date(as.character(Date),"%Y/%m%/d").
> head(df1)
Date sim obs
2 43091.25 313.62295499999999 314.39
3 43093.25 313.60034200000001 314.43
4 43094.25 313.608948 314.31
5 43095.25 313.56323200000003 314.24
6 43096.25 313.52330000000001 314.2
7 43097.25 313.47250000000003 314.29000000000002
> str(df1)
data.frame': 700 obs. of 3 variables:
$ Date: chr "43091.25" "43093.25" "43094.25" "43095.25" ...
$ sim : chr "313.62295499999999" "313.60034200000001" "313.608948"
"313.56323200000003" ...
$ obs : chr "314.39" "314.43" "314.31" "314.24" ...
There is no need to use external packages, just specify the origin Excel uses:
Date <- c("43091.25", "43093.25", "43094.25", "43095.25", "43096.25", "43097.25")
as.Date(as.numeric(Date), origin = "1899-12-30")
# [1] "2017-12-22" "2017-12-24" "2017-12-25" "2017-12-26" "2017-12-27" "2017-12-28"
Dates with a 43ddd.dd pattern usually come from Excel.
You can use openxlsx :
openxlsx::convertToDateTime(as.numeric("43091.25"))
[1] "2017-12-22 06:00:00 CET"
Waldi's answer is great and openxlsx is great. As an alternative for anyone coming across this in the future, you can also use janitor:
df <- tribble(~Date, ~sim, ~obs,
43091.25, 313.6222956, 314.39,
43093.25, 313.60034200000001, 314.43,
43094.25, 313.608948, 314.31)
df %>%
mutate(Date = janitor::excel_numeric_to_date(Date))
Produces:
Date sim obs
<date> <dbl> <dbl>
1 2017-12-22 314. 314.
2 2017-12-24 314. 314.
3 2017-12-25 314. 314.
I am having a data frame with a specific date range for each row.
stuID stdID roleStart roleEnd
1 1 7 2010-11-18 2020-06-14
2 2 2 2012-08-13 2014-04-01
3 2 4 2014-04-01 2015-10-01
4 2 3 2015-10-01 2018-10-01
5 2 6 2018-10-01 2020-06-14
6 3 4 2014-03-03 2015-10-01
I need to generate the rows based on the weeks of the date. To be precise, I need to populate the rows based on week between two dates in the given data frame.
I tried to achieve this using the following piece of code
extendedData <- reshape2::melt(setNames(lapply(1:nrow(df), function(x) seq.Date(df[x, "roleStart"],
df[x, "roleEnd"], by = "1 week")),df$stuID))
But when I execute this, I am getting the error message
Error in seq.int(0, to0 - from, by) : wrong sign in 'by' argument
This is the structure of the dataframe
'data.frame': 350 obs. of 4 variables:
$ stuID : int 1 2 2 2 2 3 3 3 4 4 ...
$ stdID : int 7 2 4 3 6 4 3 6 1 2 ...
$ roleStart: Date, format: "2010-11-18" "2012-08-13" "2014-04-01" "2015-10-01" ...
$ roleEnd : Date, format: "2020-06-14" "2014-04-01" "2015-10-01" "2018-10-01" ...
Can anyone say what's wrong with the code?
Thanks in advance!!
Here's a way to do this using tidyverse functions :
library(dplyr)
df %>%
mutate(date = purrr::map2(roleStart, roleEnd, seq, by = 'week')) %>%
tidyr::unnest(date)
As far as your code is concerned it works fine till this step i.e generating weekly dates
lapply(1:nrow(df), function(x)
seq.Date(df[x, "roleStart"], df[x, "roleEnd"], by = "1 week"))
I am not sure what you are trying to do with setNames and melt functions there.
I'm trying to convert a column of character to time. The column has observations in only minutes and seconds.
The dataset I try it on is this:
data <- data.frame(
time = c("1:40", "1:55", "3:00", "2:16"),
stringsAsFactors = FALSE
)
data
time
1 1:40
2 1:55
3 3:00
4 2:16
I have checked other questions about converting character to time on here, but haven't found a solution to my problem.
The output I want is this:
time
1 00:01:40
2 00:01:55
3 00:03:00
4 00:02:16
strptime + as.character formatting will give you the expected result. But realise that it is a character value.
data$time <- as.character(strptime(data$time, "%M:%S"), "%H:%M:%S")
data
time
1 00:01:40
2 00:01:55
3 00:03:00
4 00:02:16
Convert to chron times class and if you want a character column format it.
library(chron)
transform(data, time = times(paste0("00:", time)))
transform(data, time = format(times(paste0("00:", time))))
hms is a time class that happens to have the print method you're asking for:
data <- data.frame(
time = c("1:40", "1:55", "3:00", "2:16"),
stringsAsFactors = FALSE
)
data$time <- hms::as.hms(paste0('00:', data$time))
data
#> time
#> 1 00:01:40
#> 2 00:01:55
#> 3 00:03:00
#> 4 00:02:16
str(data)
#> 'data.frame': 4 obs. of 1 variable:
#> $ time: 'hms' num 00:01:40 00:01:55 00:03:00 00:02:16
#> ..- attr(*, "units")= chr "secs"
You can convert to hms in other ways, if you like, e.g. by parsing with as.POSIXct or strptime and then coercing with as.hms.
I know this have been asked several times but I could not find the right way to get around my problem. I have a very simple CSV file that I upload, looking like:
27.07.2015,100
28.07.2015,100.1504
29.07.2015,100.1957
30.07.2015,100.5044
31.07.2015,100.7661
03.08.2015,100.9308
04.08.2015,100.8114
05.08.2015,100.6927
06.08.2015,100.7501
07.08.2015,100.7194
10.08.2015,100.8197
11.08.2015,100.8133
Now I need to convert my data.frame into xts so I can use the PerformanceAnalytics package. My data.frame has the structure:
> str(mpey)
'data.frame': 243 obs. of 2 variables:
$ V1: Factor w/ 243 levels "01.01.2016","01.02.2016",..: 210 218 228 234 241 21 30 38 45 52 ...
- attr(*, "names")= chr "5" "6" "7" "8" ...
$ V2: Factor w/ 242 levels "100","100.0062",..: 1 4 5 10 16 20 17 13 15 14 ...
- attr(*, "names")= chr "5" "6" "7" "8" ...
I tried different things with as.xts function but could make it work.
Could you please help me get over this?
Here's a solution using the tidyquant package, which contains as_xts() for coercing data frames to xts objects and as_tibble() for coercing time series objects such as xts to tibbles ("tidy" data frames).
Recreate your data
> data_df
# A tibble: 12 × 2
date value
<fctr> <fctr>
1 27.07.2015 100
2 28.07.2015 100.1504
3 29.07.2015 100.1957
4 30.07.2015 100.5044
5 31.07.2015 100.7661
6 03.08.2015 100.9308
7 04.08.2015 100.8114
8 05.08.2015 100.6927
9 06.08.2015 100.7501
10 07.08.2015 100.7194
11 10.08.2015 100.8197
12 11.08.2015 100.8133
First, we need to reformat your data frame. The dates and values are both stored as factors and they need to be in a date and double class, respectively. We'll load tidyquant and reformat the data frame. Note that tidyquant loads the tidyverse and financial packages so you don't need to load anything else. The date can be converted with lubridate::dmy which converts characters in a day-month-year format to date. The value needs to go from factor to character then from character to double, and this is done by nesting as.numeric and as.character.
> library(tidyquant)
> data_tib <- data_df %>%
mutate(date = dmy(date),
value = as.numeric(as.character(value)))
> data_tib
# A tibble: 12 × 2
date value
<date> <dbl>
1 2015-07-27 100.0000
2 2015-07-28 100.1504
3 2015-07-29 100.1957
4 2015-07-30 100.5044
5 2015-07-31 100.7661
6 2015-08-03 100.9308
7 2015-08-04 100.8114
8 2015-08-05 100.6927
9 2015-08-06 100.7501
10 2015-08-07 100.7194
11 2015-08-10 100.8197
12 2015-08-11 100.8133
Now, we can coerce to xts using the tidyquant::as_xts() function. Just specify date_col = date.
> data_xts <- data_tib %>%
as_xts(date_col = date)
> data_xts
value
2015-07-27 100.0000
2015-07-28 100.1504
2015-07-29 100.1957
2015-07-30 100.5044
2015-07-31 100.7661
2015-08-03 100.9308
2015-08-04 100.8114
2015-08-05 100.6927
2015-08-06 100.7501
2015-08-07 100.7194
2015-08-10 100.8197
2015-08-11 100.8133
I want to generate a data frame containing dates based on the date that I choose at the beginning as ReportDate:
ReportDate <- as.Date("2014-01-01")
date <- data.frame(matrix(nrow=60, ncol=1))
for(i in 1:60){
date[i,1] = as.Date(ReportDate+i-1, origin="%Y-%m-%d")
}
but it gives me numeric values as output not date value. Please kindly tell me how I can solve this problem.
You can just add integers to Date class objects directly:
ReportDate <- as.Date("2014-01-01")
DateDf <- data.frame(
date=ReportDate+(0:59))
##
> head(DateDf)
date
1 2014-01-01
2 2014-01-02
3 2014-01-03
4 2014-01-04
5 2014-01-05
6 2014-01-06
> str(DateDf)
'data.frame': 60 obs. of 1 variable:
$ date: Date, format: "2014-01-01" "2014-01-02" "2014-01-03" ...