I have a data frame similar to this:
> var1<-c("01","01","01","02","02","02","03","03","03","04","04","04")
> var2<-c("0","4","6","8","3","2","5","5","7","7","8","9")
> var3<-c("07","41","60","81","38","22","51","53","71","72","84","97")
> var4<-c("107","241","360","181","238","222","351","453","171","372","684","197")
> df<-data.frame(var1,var2,var3,var4)
> df
var1 var2 var3 var4
1 01 0 07 107
2 01 4 41 241
3 01 6 60 360
4 02 8 81 181
5 02 3 38 238
6 02 2 22 222
7 03 5 51 351
8 03 5 53 453
9 03 7 71 171
10 04 7 72 372
11 04 8 84 684
12 04 9 97 197
I want to replace all values of the variables var2,var3,var4 with "0" that exist where var1 is 02 and/or 03. The digit number also needs to be the same so that df looks like this:
var1 var2 var3 var4
1 01 0 07 107
2 01 4 41 241
3 01 6 60 360
4 02 0 00 000
5 02 0 00 000
6 02 0 00 000
7 03 0 00 000
8 03 0 00 000
9 03 0 00 000
10 04 7 72 372
11 04 8 84 684
12 04 9 97 197
Now, I also need to be sure the command will be executed, even if var1 would not contain 02 or 03.
Basically something like if var1 contains 01 or 02 set the corresponding values in var2,var3 and var4 to 0 according to the number of digits in var2,var3 and var4 (e.g. 97 will be 00 and 197 will be 000) and if not, do nothing.
Any suggestions?