zoo

Converting yearmon column to last date of the month in R

巧了我就是萌 提交于 2019-12-02 01:09:25
I have a data frame (df) like the following: Date Arrivals 2014-07 100 2014-08 150 2014-09 200 I know that I can convert the yearmon dates to the first date of each month as follows: df$Date <- as.POSIXct(paste0(as.character(df[,1]),"-01"), format = "%Y-%m-%d") However, given that my data is not available until the end of the month I want to index it to the end rather than the beginning, and I cannot figure it out. Any help appreciated. If the Date variable is an actual yearmon class vector, from the zoo package, the as.Date.yearmon method can do what you want via its argument frac . Using

Wrong result from mean(x, na.rm = TRUE)

独自空忆成欢 提交于 2019-12-02 00:30:39
I want to compute the mean, min and max of a series of Managers returns, as follows: ManagerRet <-data.frame(diff(Managerprices)/lag(Managerprices,k=-1)) I then replace return = 0 with NaN since data are extracted from a database and not all the dates are populated. ManagerRet = replace(ManagerRet,ManagerRet==0,NaN) I have the following 3 function > min(ManagerRet,na.rm = TRUE) [1] -0.0091716 > max(ManagerRet,na.rm = TRUE) [1] 0.007565 > mean(ManagerRet,na.rm = TRUE)*252 [1] NaN Why the mean function returns a NaN value while min and max performe calculation properly? Below you can find the

Using rollapply function for VaR calculation using R

橙三吉。 提交于 2019-12-02 00:23:49
I did the following for calculating Value at Risk (VaR) over 20 period rolling window: require(PerformanceAnalytics); require(zoo) data(edhec) class(edhec) # [1] "xts" "zoo" class(edhec$CTAGlobal) # "NULL" var1<-rollapply(edhec,width=20,FUN=function(edhec) VaR(R=edhec,p=.95,method="modified"),by.column=TRUE) It produces the desired output, and then I tried the same on another data: data(managers) class(managers) # [1] "xts" "zoo" class(managers$HAM4) # [1] "xts" "zoo" var2<-rollapply(managers,width=20,FUN=function(managers) VaR(R=managers,p=.95,method="modified"),by.column=TRUE) But I am

Use of na.locf function (zoo package) with .SD in data.table

六眼飞鱼酱① 提交于 2019-12-01 23:50:38
I am trying to fill out all NA's excluding the first two NA's for cols 1 and 4 and three NA's for cols 2 and 3 with most recent non-NA value . Here is my data and code: hh<-structure(list(ka = c(NA, NA, 2, NA, NA, 3, NA, NA, NA, NA), kb = c(NA, NA, NA, 2, NA, NA, 3, NA, NA, NA), gc = c(NA, NA, NA, 3, NA, NA, 6, NA, NA, NA), hc = c(NA, NA, 8, NA, NA, NA, 4, NA, NA, NA)), .Names = c("ka", "kb", "gc", "hc" ), row.names = c(NA, -10L), class = "data.frame") library(zoo) #na.locf library(data.table) setDT(hh)[,`:=`(ka=c(NA,NA,na.locf(ka)),kb=c(NA,NA,NA,na.locf(kb)),gc=c(NA,NA,NA,na.locf(gc)),hc=c(NA

Extracting event types from last 21 day window

旧时模样 提交于 2019-12-01 23:35:21
问题 My dataframe looks like this. The two rightmost columns are my desired columns. **Name ActivityType ActivityDate Email(last 21 says) Webinar(last21)** John Email 1/1/2014 NA NA John Webinar 1/5/2014 NA NA John Sale 1/20/2014 Yes Yes John Webinar 3/25/2014 NA NA John Sale 4/1/2014 No Yes John Sale 7/1/2014 No No Tom Email 1/1/2015 NA NA Tom Webinar 1/5/2015 NA NA Tom Sale 1/20/2015 Yes Yes Tom Webinar 3/25/2015 NA NA Tom Sale 4/1/2015 No Yes Tom Sale 7/1/2015 No No I am just trying to create a

Barplot: changing x axe and adding line

你离开我真会死。 提交于 2019-12-01 22:53:01
问题 I have a zoo with daily data that looks like this: > head(almorol) 1973-10-02 1973-10-03 1973-10-04 1973-10-05 1973-10-06 1973-10-07 183.9 208.2 153.7 84.8 52.5 35.5 I want to plot annual totals and a moving average so I did: y<-apply.yearly(almorol, FUN=sum) plot(y, main="Annual totals - Tagus (Almorol)",ylab="Q (m3/s)") lines(rollapply(y, 10, mean, na.rm=TRUE), col="red", lwd=2) Which works fine, but because the data is not continuous a line graph is not correct. If I do it with points is

Extracting event types from last 21 day window

∥☆過路亽.° 提交于 2019-12-01 21:19:57
My dataframe looks like this. The two rightmost columns are my desired columns. **Name ActivityType ActivityDate Email(last 21 says) Webinar(last21)** John Email 1/1/2014 NA NA John Webinar 1/5/2014 NA NA John Sale 1/20/2014 Yes Yes John Webinar 3/25/2014 NA NA John Sale 4/1/2014 No Yes John Sale 7/1/2014 No No Tom Email 1/1/2015 NA NA Tom Webinar 1/5/2015 NA NA Tom Sale 1/20/2015 Yes Yes Tom Webinar 3/25/2015 NA NA Tom Sale 4/1/2015 No Yes Tom Sale 7/1/2015 No No I am just trying to create a yes/no variable that denotes whether there was an email or a webinar in the last 21 days for each

Trouble finding non-unique index entries in zooreg time series

最后都变了- 提交于 2019-12-01 13:32:20
I have several years of data that I'm trying to work into a zoo object (.csv at Dropbox) . I'm given an error once the data is coerced into a zoo object. I cannot find any duplicated in the index. df <- read.csv(choose.files(default = "", caption = "Select data source", multi = FALSE), na.strings="*") df <- read.zoo(df, format = "%Y/%m/%d %H:%M", regular = TRUE, row.names = FALSE, col.names = TRUE, index.column = 1) Warning message: In zoo(rval3, ix) : some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique I've tried: sum(duplicated(df$NST_DATI)) But the

R: RunningTotal in the last 365 days window by Name

别说谁变了你拦得住时间么 提交于 2019-12-01 12:51:13
This is what my data looks like. The rightmost column is my Desired Column. Name EventType EventDate SalesAmount RunningTotal Runningtotal(prior365Days) John Email 1/1/2014 0 0 0 John Sale 2/1/2014 10 10 10 John Sale 7/1/2014 20 30 30 John Sale 4/1/2015 30 60 50 John Webinar 5/1/2015 0 60 50 Tom Email 1/1/2014 0 0 0 Tom Sale 2/1/2014 15 15 15 Tom Sale 7/1/2014 10 25 25 Tom Sale 4/1/2015 25 50 35 Tom Webinar 5/1/2015 0 50 35 I am just trying to get the running total of SalesAmount for each name in the last 365 days window. For the general "RunningTotal" column I used: df<- df%>% group_by (Name)

修改Zookeeper的运行日志

守給你的承諾、 提交于 2019-12-01 12:19:53
前端时间配置zookeeper日志时出现问题,尽管修改了conf目录下的log4j.properties, 重启zookeeper后仍不起作用。 查了下资料,才知道要修改bin目录下的zkEnv.sh。 zookeper版本是3.4.5。 总结步骤如下: 1> 修改conf/log4j.properties: # Define some default values that can be overridden by system properties zookeeper.root.logger=INFO,ROLLINGFILE 2> 修改bin/zkEvn.sh文件, if [ "x${ZOO_LOG4J_PROP}" = "x" ] then ZOO_LOG4J_PROP="INFO,CONSOLE" fi 改成 if [ "x${ZOO_LOG4J_PROP}" = "x" ] then ZOO_LOG4J_PROP="INFO,ROLLINGFILE" fi 3> 还是修改bin/zkEnv.sh 这次是为了指定zookeeper.log的目录, 改下ZOO_LOG_DIR就行了,默认指向当前目录。 启动zkSever.sh start,指定目录下zookeeper.log成功出现。 来源: https://www.cnblogs.com/likethis/p