zoo

Filling NA row values with nearest right side row value in R

a 夏天 提交于 2019-12-29 09:17:32
问题 I want to convert the given dataframe from c1 c2 c3 c4 c5 VEG PUFF <NA> 12 <NA> <NA> 78.43 CHICKEN PUFF <NA> 16 <NA> 88.24 <NA> BAKERY Total <NA> <NA> 28 <NA> 84.04 to c1 c2 VEG PUFF 12 78.43 CHICKEN PUFF 16 88.24 BAKERY Total 28 84.04 I tried two methods but i didnt get accurate results it is sometimes taking left side row value step1 <- t(na.locf(t(df), fromLast=T)) step2 <- t(na.locf(t(step1), fromLast=F)) library(dplyr) MyReplace = function(data) {data %>% t %>% na.locf(.,,T) %>% na.locf

R: Holt-Winters with daily data (forecast package)

荒凉一梦 提交于 2019-12-29 09:13:34
问题 In the following example, I am trying to use Holt-Winters smoothing on daily data, but I run into a couple of issues: # generate some dummy daily data mData = cbind(seq.Date(from = as.Date('2011-12-01'), to = as.Date('2013-11-30'), by = 'day'), rnorm(731)) # convert to a zoo object zooData = as.zoo(mData[, 2, drop = FALSE], order.by = as.Date(mData[, 1, drop = FALSE], format = '%Y-%m-%d'), frequency = 7) # attempt Holt-Winters smoothing hw(x = zooData, h = 10, seasonal = 'additive', damped =

Replace missing value with previous value [duplicate]

对着背影说爱祢 提交于 2019-12-28 13:34:24
问题 This question already has answers here : Replacing NAs with latest non-NA value (15 answers) Replace missing values (NA) with most recent non-NA by group (6 answers) Closed 2 years ago . Event,Time,Bid,Offer Quote,0.458338,9.77,9.78 Order,0.458338,NA,NA Order,0.458338,NA,NA Order,0.458338,NA,NA Quote,0.458363,9.78,9.79 Order,0.458364,NA,NA I have a Data frame like this I want to write a efficient code to fill up the NA with previous Quote bid and ask, Time is sorted, and only Quote contains

Replacing zero with previous value in time series R

不打扰是莪最后的温柔 提交于 2019-12-25 09:37:10
问题 I have this time series as Quant1 Quant2 2013-01-23 400 200 2013-01-22 0 0 2013-01-21 0 0 2013-01-20 125 100 2013-01-18 120 0 And wants output as Quant1 Quant2 2013-01-23 400 200 2013-01-22 125 100 2013-01-21 125 100 2013-01-20 125 100 2013-01-18 120 0 I am trying this, but it does not seem to work. I am getting null error NULL Warning encountered while processing method replace(df,df == 0, NA) df <- na.locf(df) df[is.na(df)] <- 0 Any suggestions? Update As per most voted answer I tried (I

Replacing zero with previous value in time series R

狂风中的少年 提交于 2019-12-25 09:37:09
问题 I have this time series as Quant1 Quant2 2013-01-23 400 200 2013-01-22 0 0 2013-01-21 0 0 2013-01-20 125 100 2013-01-18 120 0 And wants output as Quant1 Quant2 2013-01-23 400 200 2013-01-22 125 100 2013-01-21 125 100 2013-01-20 125 100 2013-01-18 120 0 I am trying this, but it does not seem to work. I am getting null error NULL Warning encountered while processing method replace(df,df == 0, NA) df <- na.locf(df) df[is.na(df)] <- 0 Any suggestions? Update As per most voted answer I tried (I

How to merge list of zoo objects

僤鯓⒐⒋嵵緔 提交于 2019-12-25 04:51:08
问题 I have a list of zoo objects, and I'd like to merge them. I've already made sure that they're all the same length. It seems that in order to merge them, I need to do something like, merge(my_list[[1]],my_list[[2]]...) But this quickly becomes cumbersome as list length increases. Does someone have a suggestion on handling this? 回答1: Use do.call : do.call("merge", my_list) Note that the zoo objects do not have to be the same length. 来源: https://stackoverflow.com/questions/28160424/how-to-merge

Grouped Rolling Average by Date

对着背影说爱祢 提交于 2019-12-25 03:56:17
问题 I have data at a Day Level. Each day always has more than 1 value, and the days are not necessarily in consecutive order. I want to create a calculation for the mean on a particular day and the rolling 14 day mean. I have tried doing this in R but I am not having much luck. I think I am close though. Basically rolling mean and regular mean grouped by date. Thanks so much, I'm going crazy since I think I'm so close! library(plyr) library(zoo) help=ddply(data, .(DATE), roll_avg14 =rollmean

R - no documentation for .fill_short_gaps

南楼画角 提交于 2019-12-24 22:33:26
问题 I was looking at the R source codes for the zoo package (who many functions are extremely useful). I noticed a function .fill_short_gaps used quite a lot, but I can't find any documentation for this either in the zoo source codes or in the base source codes. Is this an internal function? What is this function supposed to do? 回答1: It's an internal function. A checkin comment on version 661 of the source file says "Use base R coding style convention for internal non-exported functions: .fill

Calculate variables using equations then use the generated values to generate new one

末鹿安然 提交于 2019-12-24 20:50:44
问题 It's hard to explain it but i'll take it step by step Let's say I've 2 cars, one following another, and I've the speed of the lead car, and I want to calculate the distance between two of them, and we can calculate the distance using multiple equations, also I know the initial speed of the following car and the distance between two of them. Following_Car_Speed = 13.68490 m/s Distance = 17.024 m Lead_Car_Speed = c(13.784896, 13.745834, 13.880556, 13.893577, 13.893577, 13.923959, 13.945661, 13

Generate time sequence of a day with a minute difference in R

天大地大妈咪最大 提交于 2019-12-24 17:45:59
问题 I want to generate time sequence of a day by a minute difference using R like 00:00, 00:01, 00:02, ..., 23:59 For the same, I am using timeBasedSeq function of xts package with following lines of code timerange1<- paste('T00:00','/','T23:59',' 12:00',sep="") timeBasedSeq(timerange1) But, I am not able to generate the sequence with this. Also, I do not understand what 12:00 mean in first line of code, i.e., how does it relate to minutes or hours or seconds. Any help will be appreciated. 回答1: