vendredi 22 novembre 2019

Find last non-zero element in column for each group, fill different column

I am trying to create a for loop that does the following:

for (i in 2:length(Exampledata$Levels)) { 
  if(is.na(Exampledata$Levels[i]) == "TRUE"    {   
    find the last instance where 
is.na(Exampledata$Levels) == "FALSE" 
  for that same ID, and input 
the day from that row into last_entry[i]
  }
}

Example data:

ID<-c("QYZ","MMM","QYZ","bb2","gm6","gm6","YOU","LLL","LLL","LLL")
day<-c(1,2,3,4,5,6,7,8,9,10)
values<-c(1,2,4,5,5,6,8,9,6,4)
Levels<-c("A","","A","C",'D','D',"C","y","","")
last_entry<-c(0,0,0,0,0,0,0,0,0,0)

What data currently looks like:

    ID values Levels day last_entry
1  QYZ      1      A   1          0
2  MMM      2          2          0
3  QYZ      4      A   3          0
4  bb2      5      C   4          0
5  gm6      5      D   5          0
6  gm6      6      D   6          0
7  YOU      8      C   7          0
8  LLL      9      y   8          0
9  LLL      6          9          0
10 LLL      4         10          0

What I want it to look like:

    ID values Levels day last_entry
1  QYZ      1      A   1          0
2  MMM      2          2          0
3  QYZ      4      A   3          0
4  bb2      5      C   4          0
5  gm6      5      D   5          0
6  gm6      6      D   6          0
7  YOU      8      C   7          0
8  LLL      9      y   8          0
9  LLL      6          9          8
10 LLL      4         10          8

I have seen a lot of code that looks for last non-zero elements or last is.na=FALSE, but none that can do it by ID, and extract a value from that row. I also need to ignore cases where there is no entry for that ID.

Essentially I want to know the last day that a level was entered for that ID.

Aucun commentaire:

Enregistrer un commentaire