Problem endpoint function R

Asked

Viewed 37 times

4

I’m working with this date.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 that?

I think the problem is in the columns of the dates that are factors. But I couldn’t fix it:

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

Thank you.

1 answer

3


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

You also need to inform the na.string = "ND", since the data is recorded with ND as NA. Once done, you need to convert the date columns to 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:

  1. Always put the packages you are using, if you have code that is not from base-R.
  2. Make sure your code is reproducible, you started using St and changed to St_GBP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.