sebzee
sebzee

Reputation: 35

Conditionally changing cells in a row in R

Hello Stack Overflow community!

I'm stuck on a formatting step in R. I have a large dataset where each row represents an individual radiocollared animal, and each column is the state of the individual on a given date. I want all NA's before the capture of the animal to be 0's, and all NA's after the individual dies to be 4's. There are NA's in between capture and death and I want those to stay NA's. Mortality is signified by a 4, and capture is signified as the first number >0 and <4.

Here is an example of the data:

practice.df <- data.frame("200401"=c(NA,NA,1,1),
                          "200402"=c(2,1,NA,4),
                          "200403"=c(1,3,4,NA))

And here is what I'm going for:

goal.df <- data.frame("200401"=c(0,0,1,1),
                      "200402"=c(2,1,NA,4),
                      "200403"=c(1,3,4,4))

Essentially, I want a statement where R fills in with a 0 until it hits a number >0, then stops, and then fills in with a 4 after it hits a cell ==4.

I'm just not that great with R and don't really know where to start on this problem (if/else maybe?) - any advice would be hugely helpful!

Thank you!

Upvotes: 0

Views: 50

Answers (1)

eonurk
eonurk

Reputation: 547

I got your question now:

for(i in seq_len(nrow(practice.df))){
    any.number.occured = F
    four.occured = F
    for(j in seq_len(ncol(practice.df))){
        if(is.na(practice.df[i,j])){ # is an NA
            if(any.number.occured){
                if (four.occured){
                    practice.df[i,j] <- 4
                }
            } else {
                practice.df[i,j] <- 0
            }
        } else { # is a number
            if (practice.df[i,j] == 4) {
                four.occured = T
            }
            any.number.occured = T
        }
    }
}

You can check out the code, I think it is self explanatory.

Upvotes: 2

Related Questions