zoo

Using na.locf to carry last value forward ignoring first rows when first observation is na

允我心安 提交于 2019-12-11 07:29:15
问题 I would like to make use of the na.locf to carry forward non-missing values for data frames where first observation may be zero . Problem dta <- data.frame(A = c(NA, NA, 1, 2, 4, 5, NA, NA, NA), B = c(NA, 5, 4, 5, 8, 9, NA, NA, 100)) dta %>% mutate_all(.funs = funs(na.locf(.))) Error in mutate_impl(.data, dots) : Column A must be length 9 (the number of rows) or one, not 7 Desired results Vectorize(require)(package = c("dplyr", "zoo"), character.only = TRUE) dta <- data.frame(A = c(0, NA, 1,

Plotting xts objects works with points but not with lines

跟風遠走 提交于 2019-12-11 04:34:59
问题 I have merged two xts objects and want to plot them in a single display. This works fine when I use points (type="p"). However, when I use lines (type="l") a problem occurs: the first series is shown only in the index region that is not covered by the second series. I would expect the lines to be as long as the "points". A reproducible example is posted below. As this occurs with both the default and the ggplot plotting commands, I suspect that this relates to some property of time-series

Convert times series to.quarterly with NAs

。_饼干妹妹 提交于 2019-12-11 02:37:35
问题 I have a multivariate zoo time series that contains NAs and I want to convert that to a quarterly series. df1 <-1:12 df1[df1%%4==0] <- NA zoo.object <- zoo(matrix(df1, ncol=2),as.Date("2013-01-01")+(0:5)*35) colnames(zoo.object) <-c("stock1","stock2") > zoo.object stock1 stock2 2013-01-01 1 7 2013-02-05 2 NA 2013-03-12 3 9 2013-04-16 NA 10 2013-05-21 5 11 2013-06-25 6 NA Ideally, I would like to keep the earlier data in the quarter for each stocks. I have tried to.quarterly from the xts

Geom_area order in ggplot

限于喜欢 提交于 2019-12-11 01:56:25
问题 I have sample cohort data that is not color-coded or displayed in proper chronological order when plotted with ggplot() . The following code is used to generate the plot: library(ggplot2) blues <- colorRampPalette(c('lightblue', 'darkblue')) p <- ggplot(cohort.chart, aes(x=month, y=users, group=cohort)) p + geom_area(aes(fill = cohort)) + scale_fill_manual(values = blues(12)) + ggtitle('Users by cohort') + theme(plot.title = element_text(hjust = 0.5)) + theme(axis.text.x = element_text(angle

How to plot xts in ggplot2?

試著忘記壹切 提交于 2019-12-11 01:45:50
问题 I got this plot using this code library(xts) library(zoo) plot.new() par(mai=c(2, 2, 1, 1)) plot(nats[,2], ylim=c(0, 2.5), xlab = "", ylab="", main="", major.ticks="months", major.format="%d-%b-%Y", minor.ticks=FALSE, las=1, cex.axis=0.8) points(nats[rans,2], col="darkgrey", pch=16, cex=0.5) points(M1[rans], col="red", pch=16, cex=0.5) points(M2[rans], col="blue", pch=16, cex=0.5) points(M3[rans], col="green", pch=16, cex=0.5) legend(x="topright", legend=c("a", "Actual value", "M1","M2","M3")

zoo objects and millisecond timestamps

别等时光非礼了梦想. 提交于 2019-12-11 01:01:21
问题 quick question on tick data. I have tons of data under this format which I believe is perfect for what I'm trying to achieve. I want to keep some granularity in order to be able to trigger buy/sell signal under a second. data SYMBOL TIMESTAMP STAMP PRICE SIZE EXCHANGE BID BIDEX BIDSIZE ASK ASKEX ASKSIZE 1 SPXU 1330938005 1330938005000000 NA NA 9.99 PSE 5 10.10 PSE 6 2 SPXU 1330938221 1330938221000000 NA NA 9.99 PSE 5 10.19 PSE 1 3 SPXU 1330938221 1330938221000001 10.1000 600 PSE NA NA NA NA 4

How to subset a data frame by the last day of each month

最后都变了- 提交于 2019-12-10 19:44:54
问题 I have a df : dates V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 1999-05-31 66 65 64 63 62 61 60 59 58 57 1999-06-01 67 66 65 64 63 62 61 60 59 58 1999-06-02 68 67 66 65 64 63 62 61 60 59 1999-06-03 69 68 67 66 65 64 63 62 61 60 1999-06-04 70 69 68 67 66 65 64 63 62 61 1999-06-17 79 78 77 76 75 74 73 72 71 70 1999-06-18 80 79 78 77 76 75 74 73 72 71 1999-06-21 81 80 79 78 77 76 75 74 73 72 1999-06-22 82 81 80 79 78 77 76 75 74 73 1999-06-23 83 82 81 80 79 78 77 76 75 74 1999-06-24 84 83 82 81 80 79 78 77

zoo/xts microsecond read issue

白昼怎懂夜的黑 提交于 2019-12-10 18:52:12
问题 The data looks like Time Set1 Set2 10:19:38.551629 16234 16236 10:19:41.408010 16234 16236 10:19:47.264204 16234 16236 I am trying to load this into zoo. orig <- read.zoo("~/sample.txt",sep="",header=TRUE,index.column=1,format="%H:%M:%S.%6f") Error in read.zoo("~/sample.txt", sep = "", header = TRUE, index.column = 1, : index has 3 bad entries at data rows: 1 2 3 ... I have checked all the relevant posts 1. R issue with rounding milliseconds 2. Milliseconds puzzle when calling strptime in R 3

Using zoo's rollsum within data.table on timestamped transactions

不羁的心 提交于 2019-12-10 15:59:40
问题 Somewhat related to this question and this one, I'm having trouble calculating a rolling sum. Unlike those questions, I would like to try to use zoo:rollsum as analogous to the rollapply answer here. (But if there is a more data.table way to do it, by all means.) Let's start with some data: set.seed(123) some_dates <- function(){as.Date('1980-01-01') + sort(sample.int(1e4,100))} d <- data.table(cust_id = c(rep(123,100),rep(456,100)), purch_dt = c(some_dates(), some_dates()), purch_amt = round

Counting previous rows in a data table based on date

人盡茶涼 提交于 2019-12-10 15:44:24
问题 (Apologies if some of the terminology here is off - I come from a SQL background and I am only just getting into the R world) I have a data table with a series of date-ordered entries. One of the fields in the data table is a grouping value, and one is a time value. With the data ordered (or keyed - I'm new to R and still not sure of the difference) by the group THEN the date, I want to count, for each row HOW MANY rows in this group precede the current row (including the current), within a