I have been trying to assign NAs by using for loop, but is not working and I know there are possible easiest ways to do this.
I want to create an extra column (just like the column in the example named Desire_Output) in which I will assign NA to any row that in the Value column has a number higher than 1. Also, I want to assign NAs to the next following two rows. If there are NAs in the Value column, just also put NAs in the desire output column.
Here is the example:
Event<- c(1,2,2,2,2,2,2,3,3,4,4,4,4,4,5,6,6,6,7)
Value<- c(5,3,0,0,0,2,0,1,10,0,0,NA,NA,NA,1,0,8,0,0)
Desire_output<- c(NA,NA,NA,NA,0,NA,NA,NA,NA,NA,NA,NA,NA,NA,1,0,NA,NA,NA)
A<- data.frame(Event,Value,Desire_output)
Event Value Desire_output
1 1 5 NA
2 2 3 NA
3 2 0 NA
4 2 0 NA
5 2 0 0
6 2 2 NA
7 2 0 NA
8 3 1 NA
9 3 10 NA
10 4 0 NA
11 4 0 NA
12 4 NA NA
13 4 NA NA
14 4 NA NA
15 5 1 1
16 6 0 0
17 6 8 NA
18 6 0 NA
19 7 0 NA
This is what I tried to do, but when I getvto the NAs in the Value column I started to have some troubles.
for (f in 1:(nrow(A)-1)){
if(A$Value2[f] > 1){
A$Value2[f]<- NA
A$Value2[f+1]<- NA
A$Value[f+2]<- NA
}else{
}
}
Please let me know if you have an easiest way to do it with any other method.
Aucun commentaire:
Enregistrer un commentaire