Generate repeated values in R

3

In the frame date below, I want the days that have no gain information to be repeated based on the last information collected. For example from day 2 to day 15 the gain will be 0.97 and day 79 to 86 of 0.45 How can I do this in R?

Dia	Ganho
2	0.97
16	0.95
23	1.03
24	0.99
30	1.03
37	1.01
44	1.11
51	1.04
58	1.31
65	1.17
72	1.09
79	0.45
86	
    
asked by anonymous 08.11.2017 / 11:15

2 answers

4

The zoo package contains the na.locf function that does exactly what you need. According to its description, it is a generic function that replaces every NA with the most recent non-NA previous. Follow the code using this function

df <- data.frame(
  Dia = c(2,16,23,24,30,37,44,51,58,65,72,79,86),
  Ganho = c(.97,.95,1.03,.99,1.03,1.01,1.11,1.04,1.31,1.17,1.09,.45,NA)
)
temp <- expand.grid(Dia = seq(from = min(df$Dia), to = max(df$Dia), by = 1))
df <- merge(df, temp, all = T)
library(zoo)
df <- na.locf(df)
    
08.11.2017 / 13:35
4

Try the code below. I think that's what you want.

resultado <- lapply(seq_along(dados$Dia)[-nrow(dados)], function(i) {
    data.frame(Dia = seq(dados$Dia[i], dados$Dia[i + 1] - 1), Ganho = dados$Ganho[i])
})
resultado <- do.call(rbind, resultado)
resultado <- rbind(resultado, data.frame(Dia = dados$Dia[nrow(dados)],
                                         Ganho = resultado$Ganho[nrow(resultado)]))
resultado

DATA.

dados <-
structure(list(Dia = c(2L, 16L, 23L, 24L, 30L, 37L, 44L, 51L, 
58L, 65L, 72L, 79L, 86L), Ganho = c(0.97, 0.95, 1.03, 0.99, 1.03, 
1.01, 1.11, 1.04, 1.31, 1.17, 1.09, 0.45, NA)), .Names = c("Dia", 
"Ganho"), class = "data.frame", row.names = c(NA, -13L))
    
08.11.2017 / 13:27