简体   繁体   中英

converting data frame (factors) into xts

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM