Generate repeated values in R

Asked

Viewed 126 times

3

In the frame date below I want those on days that have no gain information that the gain is repeated based always on the last collected information. For example from day 2 to day 15 the gain will be 0.97 and from 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	

2 answers

5

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

DICE.

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))
  • I ran your code here and it seemed to me that there are lines with repeated days on resultado, but different gains.

  • @Marcusnunes You’re right. Error corrected.

4


The package zoo contains the function na.locf that does exactly what you need. According to its description, it is a generic function that replaces each NA with the latest non-NA previous. Follows 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)
  • This function is suitable for what I need. In this example I passed has information of gain of only one animal. You could tell me how to apply this function that Oce passed to a dataset with several animals (100 animals). I thought about using a loop (for animal in 1 1:100), but I don’t know how to apply in this function

  • This function works for a data.frame with more than one column. I’ll even edit my answer since in it, I apply specifically in the variable Ganho

Browser other questions tagged

You are not signed in. Login or sign up in order to post.