Endpoint function problem R

4

I'm working with this data.frame:

St <- data.frame(read.csv2("interest_LastMonthDay.csv"))
        Date     AAA_S.t.    Date.1 BBB_S.t.    Date.2 CCC_S.t.
1   27/12/88      1,80400  28/12/88   0,8368  28/12/88   0,0080
2   28/12/88      1,78900  29/12/88   0,8386  29/12/88   0,0079
3   29/12/88      1,78950  30/12/88   0,8384  30/12/88   0,0080
4   30/12/88      1,80850  2/01/89        ND  2/01/89        ND
5   2/01/89           ND   3/01/89    0,8398  3/01/89    0,0081
6   3/01/89       1,82250  4/01/89    0,8389  4/01/89    0,0080
7   4/01/89       1,80700  5/01/89    0,8407  5/01/89    0,0080
8   5/01/89       1,79700  6/01/89    0,8367  6/01/89    0,0079
9   6/01/89       1,78000  9/01/89    0,8354  9/01/89    0,0079
10  9/01/89       1,76360  10/01/89   0,8327  10/01/89   0,0079
11  10/01/89      1,76370  11/01/89   0,8328  11/01/89   0,0079
12  11/01/89      1,78100  12/01/89   0,8356  12/01/89   0,0079
13  12/01/89      1,78400  13/01/89   0,8342  13/01/89   0,0079

I want to leave only the last values of each month. For this I used the endpoint function:

St_GBP<-St[,c(1:2)];St_GBP<-St_GBP[endpoints(St_GBP$Date, on = "months"), ]

It returns me the following error:

Error in try.xts(x, error = "must be either xts-coercible or timeBased") : 
  must be either xts-coercible or timeBased

How can I get around this?

I think the problem is in the date columns that are factors . But I could not resolve:

class(St$Date);class(St$Date.1);class(St$Date.2)
[1] "factor"
[1] "factor"
[1] "factor"

Thank you.

    
asked by anonymous 24.06.2016 / 19:10

1 answer

3

Your problem is reading the data. First, you need to use stringsAsFactors = FALSE so that R does not convert the non-numeric columns into factors.

You also need to enter the na.string = "ND" , since the data is written with ND as NA . After that, you need to convert the date columns to the time format that R recognizes, using strptime . The complete code (reading the data from the text) looks like this:

St <- read.table(text="Date     AAA_S.t.    Date.1 BBB_S.t.    Date.2 CCC_S.t.
1   27/12/88      1,80400  28/12/88   0,8368  28/12/88   0,0080
2   28/12/88      1,78900  29/12/88   0,8386  29/12/88   0,0079
3   29/12/88      1,78950  30/12/88   0,8384  30/12/88   0,0080
4   30/12/88      1,80850  2/01/89        ND  2/01/89        ND
5   2/01/89           ND   3/01/89    0,8398  3/01/89    0,0081
6   3/01/89       1,82250  4/01/89    0,8389  4/01/89    0,0080
7   4/01/89       1,80700  5/01/89    0,8407  5/01/89    0,0080
8   5/01/89       1,79700  6/01/89    0,8367  6/01/89    0,0079
9   6/01/89       1,78000  9/01/89    0,8354  9/01/89    0,0079
10  9/01/89       1,76360  10/01/89   0,8327  10/01/89   0,0079
11  10/01/89      1,76370  11/01/89   0,8328  11/01/89   0,0079
12  11/01/89      1,78100  12/01/89   0,8356  12/01/89   0,0079
13  12/01/89      1,78400  13/01/89   0,8342  13/01/89   0,0079", 
                 dec = ",", na.strings = "ND", stringsAsFactors = FALSE)

St$Date <- strptime(St$Date, format="%d/%m/%y")

library(xts)
St[endpoints(St$Date, on = "months"), ]

#         Date AAA_S.t.   Date.1 BBB_S.t.   Date.2 CCC_S.t.
#4  1988-12-30   1.8085  2/01/89       NA  2/01/89       NA
#13 1989-01-12   1.7840 13/01/89   0.8342 13/01/89   0.0079

PS: Two tips for when to ask questions:

  • Always place the packages you are using, if you have non-R-based code.
  • Make sure your code is reproducible, you started using St and changed to St_GBP .
  • 25.06.2016 / 04:52