0

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

dnatcha
  • 9
  • 3
  • 1
    when you replace the 0s with 1s, try `run.length$values[pmin(length(run.length$lengths), which(run.length$values == 1 & run.length$lengths > 1) + 1)] = 1` instead – rawr Jan 06 '21 at 23:20
  • Thank you it work well! however, in my dataframe i also have NA and i wanted to keep the NA. but this adjustment treated NA as 0 and replace it with 1 when its between 1 ie, 1, NA, 1 Is there an extra command to make them ignore NA but not exlude it? thank you ! – dnatcha Jan 12 '21 at 13:37

0 Answers0