Here is my dataset:
> p
ManagerName Employee.Name Employee.ID MaxDate Batch
1 Abarrientos, Claire Vinnikov, Olga 32403 8/11/2015 Batch.47
2 Adel, Bonnie Adams, Tracy 201850
3 Adel, Bonnie Black, Chantal 213746 7/29/2011 Batch.17
4 Adel, Bonnie Brandoli, Morena 201990 7/29/2011 Batch.17
5 Adel, Bonnie Campbell, Melissa 201931
X.New.Employee.EHS.Document.Sign.off Batch.01 Batch.02 Batch.03 Batch.04
1 1 1 1 1 1
2 NA 1 1 1 1
3 1 1 1 1 1
4 1 1 1 1 1
5 NA 1 1 1 1
Batch.06 Batch.07 Batch.08 Batch.09 Batch.10 Batch.11 Batch.18 Batch.19
1 1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1 1
3 1 1 1 1 1 1 1 1
4 1 1 1 1 1 1 1 1
5 1 1 1 1 1 1 1 1
Batch.20 Batch.22 Batch.24 Batch.25 Batch.26 Batch.27 Batch.28 Batch.29
1 1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1 1
3 1 1 1 1 1 1 1 1
4 1 1 1 1 1 1 1 1
5 1 1 1 1 1 1 1 1
Batch.30 Batch.31 Batch.32 Batch.33 Batch.34 Batch.35 Batch.36 Batch.37
1 1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1 1
3 1 1 1 1 1 1 1 1
4 1 1 1 1 1 1 1 1
5 1 1 1 1 1 1 1 1
Batch.38 Batch.39 Batch.40 Batch.41 Batch.42 Batch.43 Batch.44 Batch.45
1 1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1 1
3 1 1 1 1 1 1 1 1
4 1 1 1 1 1 1 1 1
5 1 NA NA NA NA NA NA NA
Batch.46 Batch.47
1 1 1
2 1 1
3 NA NA
4 1 1
5 NA NA
I'm a novice R user and I'm trying to figure out how to change all columns values to 1 given the Batch Number. For example, for the first row, the Batch Number is "Batch.47". Therefore, I want to replace all the values from the columns "Batch.1", "Batch.2", "Batch.3" and all the way through to "Batch.47" to "1". However, I only want to do this for rows that have a value of "1" under the New.Employee.Sign.Off column.
Here's my code thus far:
for (i in 1:nrow(p))
{if (p$X.New.Employee.EHS.Document.Sign.off[i]==1)
{k<-which(colnames(p)==p$Batch[i])
p[i,]<-replace(p[i,],6:k[i],1)
i=i+1}
else if (is.na(p$X.New.Employee.EHS.Document.Sign.off[i]))
{i=i+1}}
%This produces the following error%
Error in if (p$X.New.Employee.EHS.Document.Sign.off[i] == 1) { :
missing value where TRUE/FALSE needed
Would really appreciate any guidance. Much appreciated and thanks so very much in advance!
Aucun commentaire:
Enregistrer un commentaire