Hi I have a similar question to [text] (Replace values in data frame from column of indexes) I have a data frame that looks like the following.
COL1 <- c(1,1,1,NA,1,1)
COL2 <- c(1,NA,NA,1,1,1)
COL3 <- c(1,1,1,1,1,1)
index <- c(2,3,2,3,2,2)
Data <- data.frame (COL1, COL2, COL3, index)
Data
COL1 COL2 COL3 index
1 1 1 2
1 NA 1 3
1 NA 1 2
NA 1 1 3
1 1 1 2
1 1 1 2
Where "index" tells me a certain threshold. For each row I would like to replace all column values up to the value indicated by "index" with a unique value like 99. So I would end up with something like
COL1 COL2 COL3
99 99 1
99 99 99
99 99 1
99 99 99
99 99 1
99 99 1
From the previous question the answer to change the instances after the "index" would be
t(apply(Data, 1, function(x) {
i1 <- match( x[4], names(x)[-4])+1
i1[i1>3] <- 0
i1 <- if(i1!=0) i1:3 else i1
c(replace(x[-4], i1, 99), x[4])}))
However this throws me errors
"Error in if (i1 != 0) i1:3 else i1 : missing value where TRUE/FALSE needed"