Krug
Krug

Reputation: 1013

Express column as percent (xts)

There is an xts object with prices on which I calculate the log returns between periods/rows. I want to express the returns in percentage. The approach recommended here (i.e. using percent function of scales package) converts returns into a vector of characters. I want to convert returns to a vector of numbers, not characters.

So I used percent function of formattable package, which supposedly does that. Unfortunately, I get an error message and I'm stuck. Would anyone know what the issue is? Thanks in advance.

library('formattable')
DTT3 <- structure(c(126.33, 126.37, 126.42, 126.3, 126.31, 126.31, 126.31,126.37, 126.41, 126.42, 126.38, 126.33, 126.38, 126.38, 126.36, 126.39, 126.42, 126.42, 126.43, 126.43, 126.42, 126.39, 126.47, 126.56, 126.53, 126.5, 126.43, 126.45, 126.5, 126.42, 126.39, 126.39, 126.37, 126.24, 126.21, 126.15, 126.21, 126.24, 126.24, 126.25, 126.25, 126.2, 126.18, 126.21, 126.15, 126.16, 126.03, 125.94, 125.97, 125.97, 125.7, 125.73, 125.73, 125.55, 126.38, 126.47, 126.43, 126.35, 126.32, 126.37, 126.37, 126.43, 126.44, 126.43, 126.39, 126.39, 126.42, 126.38, 126.38, 126.44, 126.44, 126.44, 126.44, 126.44, 126.42, 126.5, 126.56, 126.57, 126.55, 126.52, 126.56, 126.5, 126.52, 126.44, 126.43, 126.42, 126.41, 126.29, 126.22, 126.29, 126.24, 126.3, 126.25, 126.27, 126.3, 126.2, 126.25, 126.21, 126.2, 126.17, 126.03, 126.04, 126.02, 125.97, 126.04, 125.89, 125.81, 125.6, 126.32, 126.36, 126.3, 126.25, 126.26, 126.29, 126.29, 126.36, 126.4, 126.37, 126.32, 126.28, 126.36, 126.34, 126.36, 126.39, 126.36, 126.37, 126.41, 126.41, 126.35, 126.39, 126.43, 126.52, 126.48, 126.42, 126.4, 126.41, 126.36, 126.34, 126.35, 126.3, 126.22, 126.2, 126.14, 126.15, 126.15, 126.22, 126.18, 126.23, 126.15, 126.14, 126.15, 126.15, 126.09, 125.91, 125.87, 125.94, 125.9, 125.7, 125.67, 125.63, 125.54, 125.35, 126.37, 126.42, 126.3, 126.32, 126.3, 126.32, 126.36, 126.41, 126.42, 126.38, 126.33, 126.39, 126.38, 126.36, 126.38, 126.42, 126.44, 126.42, 126.44, 126.41, 126.4, 126.47, 126.55, 126.53, 126.5, 126.43, 126.45, 126.49, 126.42, 126.39, 126.38, 126.36, 126.25, 126.22, 126.16, 126.21, 126.23, 126.24, 126.25, 126.26, 126.21, 126.18, 126.2, 126.15, 126.16, 126.02, 125.95, 125.98, 125.97, 125.7, 125.73, 125.72, 125.55, 125.38, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts", "zoo"), index = structure(c(1403483400, 1403484300, 1403485200, 1403486100, 1403487000, 1403487900, 1403488800, 1403489700, 1403490600, 1403491500, 1403492400, 1403493300, 1403494200, 1403495100, 1403496000, 1403496900, 1403497800, 1403498700, 1403499600, 1403500500, 1403501400, 1403502300, 1403503200, 1403504100, 1403505000, 1403505900, 1403506800, 1403507700, 1403508600, 1403509500, 1403510400, 1403511300, 1403512200, 1403513100, 1403514000, 1403514900, 1403515800, 1403516700, 1403517600, 1403518500, 1403519400, 1403520300, 1403521200, 1403522100, 1403523000, 1403523900, 1403524800, 1403525700, 1403526600, 1403527500, 1403528400, 1403529300, 1403530200, 1403531100), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(54L, 5L), .Dimnames = list(NULL, c("Open", "High", "Low", "Close", "S_Base")))
lr <- diff(log(DTT3$Close))
lrpct <- percent(lr$Close,digits =2,format ="f")

#ERROR MESSAGE:
Error in create_obj(x, "formattable", list(formatter = formatter, format = list(...),  : 
             argument "formatter" is missing, with no default

Upvotes: 2

Views: 460

Answers (1)

AK88
AK88

Reputation: 3026

Is this what you want:

lrpct <- percent(lr, digits =2,format ="f", formatter = "formatC")
lrpct

## [1] 0.04%  -0.09% 0.02%  -0.02% 0.02%  0.03%  0.04%  0.01%  -0.03% -0.04%
## [11] 0.05%  -0.01% -0.02% 0.02%  0.03%  0.02%  -0.02% 0.02%  -0.02% -0.01%
## [21] 0.06%  0.06%  -0.02% -0.02% -0.06% 0.02%  0.03%  -0.06% -0.02% -0.01%
## [31] -0.02% -0.09% -0.02% -0.05% 0.04%  0.02%  0.01%  0.01%  0.01%  -0.04%
## [41] -0.02% 0.02%  -0.04% 0.01%  -0.11% -0.06% 0.02%  -0.01% -0.21% 0.02% 
## [51] -0.01% -0.14% -0.14%

Upvotes: 2

Related Questions