Quiero calcular las diferencias entre celdas en un marco de datos de una columna.
> head(df_benchmark)
Date .STOXXR
1 2000-01-03 478.52
2 2000-01-04 459.53
3 2000-01-05 448.19
4 2000-01-06 446.24
5 2000-01-07 455.80
6 2000-01-10 462.97
El archivo esta aquí.
Así que intenté lo siguiente:
> df_benchmark <- read.xlsx("Data.xlsx", sheet = "Benchmark", startRow = 2,colNames = TRUE, detectDates = TRUE, skipEmptyRows = FALSE)
> df_benchmark[2] <- data.matrix(df_benchmark[2])
> library(lubridate)
> percent_change2 <- function(x)last(x)/first(x) - 1
> monthly_return_benchmark <- df_benchmark %>%
+ group_by(gr = floor_date(Date, unit = "month")) %>%
+ summarize_at(vars(-Date, -gr), percent_change2) %>%
+ ungroup() %>%
+ select(-gr) %>%
+ as.matrix()
Sin embargo, tengo un error. Parece que mi marco de datos con una columna de fechas y una columna de valores debe ser un vector o lista atómica 1d. Parece que la razón es un problema de variables:
Error: Each variable must be a 1d atomic vector or list.
Problem variables: '.STOXXR'
Segunda intento
Así que probé también el siguiente código:
library(xts)
df_benchmark <- read.xlsx("Data.xlsx", sheet = "Benchmark", startRow = 2,colNames = TRUE, detectDates = TRUE, skipEmptyRows = FALSE)
percent_change2 <- function(x) last(x)/first(x) - 1
monthly_return_benchmark <- df_benchmark %>%
group_by(gr = floor_date(as_date(Date), unit = "month")) %>%
summarize_at(vars(-Date, -gr), percent_change2) %>%
ungroup()
# Generamos el xts, indicando la columna con la info de tiempo
monthly_return_benchmark <- xts(monthly_return[,-1], order.by=monthly_return$gr)
Pero obtengo el siguiente error:
> library(xts)
> df_benchmark <- read.xlsx("Data.xlsx", sheet = "Benchmark", startRow = 2,colNames = TRUE, detectDates = TRUE, skipEmptyRows = FALSE)
>
> percent_change2 <- function(x) last(x)/first(x) - 1
> monthly_return_benchmark <- df_benchmark %>%
+ group_by(gr = floor_date(as_date(Date), unit = "month")) %>%
+ summarize_at(vars(-Date, -gr), percent_change2) %>%
+ ungroup()
Error in eval(substitute(expr), envir, enclos) :
non-numeric argument to binary operator
Tercera intento
Solo usando el group_by
usando los meses como unidades obtengo lo siguiente:
> monthly_return_benchmark <- df_benchmark %>%
+ group_by(gr = floor_date(Date, unit = "month"))
> monthly_return_benchmark
Source: local data frame [4,604 x 3]
Groups: gr [216]
Date .STOXXR gr
<date> <chr> <date>
1 2000-01-03 478.52 2000-01-01
2 2000-01-04 459.53 2000-01-01
3 2000-01-05 448.19 2000-01-01
4 2000-01-06 446.24 2000-01-01
5 2000-01-07 455.8 2000-01-01
6 2000-01-10 462.97 2000-01-01
7 2000-01-11 459.85 2000-01-01
8 2000-01-12 459.84 2000-01-01
9 2000-01-13 462.27 2000-01-01
10 2000-01-14 473.43 2000-01-01
# ... with 4,594 more rows
Ahora tengo que hacer el percent_change2
pero nunca funcionó.
Actualización
No sé porqué pero apareció un nueva error. Parece que df_benchmark
no tiene un appropriate time-based object
> df_benchmark %>%
+ #mutate(.STOXXR = as.numeric(.STOXXR)) %>% #Corrige el problema leyendo con read.excel (no testeado)
+ group_by(gr = floor_date(as_date(Date), unit = "month")) %>%
+ summarize(retorno = last(.STOXXR)/first(.STOXXR) -1) %>% #Defino la función directamente acá
+ ungroup() -> monthly_return_benchmark #Asigno nombre al final
>
> monthly_return_benchmark <- xts(monthly_return[,-1], order.by=monthly_return$gr) #No testeado, no uso esa librería#No testeado, no uso esa librería
Error in xts(monthly_return[, -1], order.by = monthly_return$gr) :
order.by requires an appropriate time-based object
Pienso que hay un error: debe ser utilizado:
monthly_return_benchmark <- xts(monthly_return_benchmark[,-1], order.by=monthly_return_benchmark$gr) #No testeado, no uso esa librería#No testeado, no uso esa librería
character
, por eso no te pasa el error denon-numeric