i have encontered some problem when i try to replace some value in my dataframe using rle. My dataset is recorded infection status in chicken. The first few columns are descriptive such as ID, isolators and treatments. the main problem i have is from column 4 to 14 which recorded the status of the chicken(0,1) in each sampling time. I simplified the dataset in the following code
df1 <- data.frame("ID" = c(1,2,3,4,5,6,7,8,9,10),
"Isolator" = c(1,1,1,1,1,1,1,1,1,1),
"tre"= c("a","a","a","a","a","a","a","a","a","a"),
"T1" = c(0,0, 0, 0, 0, 0,0,0,0,0),
"T2" = c(0,1, 1, 0, 1, 0,1,1,0,1),
"T3"= c(0,0, 0, 1, 1, 1,1,0,1,1),
"T4"= c(1,1, 1, 1, 0, 1,1,1,1,1),
"T5"= c(1,1, 1, 0, 1, 1,1,1,1,0),
"T6"= c(1,0, 1, 1, 0, 0,1,0,1,1))
i need to adjust the status based on its occurence. that is if one 1 occured followed by 0 -> replace 1 with 0 if more than 1 occured and followed by 0 -> replace the following 0 with 1
ie. input
v = c(NA,0,0,0,1,0,1,1,0,1,1)
output
v = c(NA,0,0,0,0,1,0,1,1,1,1)
I used rle() to locate these positions
run.length = rle(v)
run.length$values[which(run.length$values == 1 & run.length$lengths == 1) ] = 0
run.length$values[which(run.length$values == 1 & run.length$lengths > 1) +1] = 1
inverse.rle(run.length)
##The first rle worked
run.length$values[which(run.length$values == 1 & run.length$lengths == 1) ] = 0
=> v = c(NA,0,0,0,0,0,0,1,1,1,1)
but the second rle() that is
run.length$values[which(run.length$values == 1 & run.length$lengths>1) +1] = 1
did not work and showed the following warning message
Warning message: In run.length$values == 1 & run.length$lengths > 1 : longer object length is not a multiple of shorter object length
I also tried to apply it to my dataframe and similar problem occur.
A<-apply(rawBnew[6:15], 1, function(x) {
run.length = rle(x)
run.length$values[which(run.length$values == 1 & run.length$lengths == 1)] = 0
run.length$values[which(run.length$values == 1 & run.length$lengths > 1) +1] = 1
inverse.rle(run.length)
})
error still occured when i included the second rle the warning message is Error in inverse.rle(run.length) : invalid 'rle' structure