I have a large data frame (df) with binomial values ranging from 1 to 2. NA is also included in the data. As a practical example, I will create a short vector containing a subset of user data:
df <- c(NA,NA,2,1,1,1,2,1,2,2,1,1,1,NA,2,2,1,2,1,1,1,2)
In principle, I would like to get the result - this is a function that searches for the first and second 2arrays and converts everything within this interval to 2. Nevertheless, if the difference between the positions of the second and first 2 is equal to> 3, then the values remain what they are is, and no changes are happening.
In addition to the above, the function must have a loop for each value df. For example, consider the case again:
df <- c(NA,NA,2,1,1,1,2,1,2,2,1,1,1,NA,2,2,1,2,1,1,1,2)
The function should have the following result:
df_outcome <- c(NA,NA,2,1,1,1,2,2,2,2,1,1,1,NA,2,2,2,2,1,1,1,2)
, df_outcome 2 , a > 3. , , 2.
( ):
rollapply zoo , 2 , .
func <- function (q) {
for (i in (which(q %in% 2)[1]):(which(q %in% 2)[2])) {
q[i]<-2
}
return(q)
}
rollapply, , ().
df_outcome<-rollapply(df, width = 3, FUN = func, fill = NA, partial = TRUE, align = "left")
, , . , rollapply, :
( (q% % 2) [1]):( (q% % 2) [2]): NA/NaN : FUN ( [replace (posns,! Ix, 0)],...)
, rollapply , , , , . rollapply, . , , , User_ID ( .variables ddply by data.table).
.