lundi 26 janvier 2015

r- if inside lapply

I have a table like:



dir E_numdir last2
1 a 1 1
2 PJE INDEPENDENCIA 96 5 96, 5 96, 5
3 PJE INDEPENDENCIA 96 5 96, 5 96, 5
4 B 34 VIVIENDA RECRE 53 00 34, 53, 00 34, 53, 00
5 SARMIENTO CASA DE GO 613 613 13
6 ROCA 958 00 o 958, 00 58, 00


I need to evaluate each element of each row and if they are 00 store a list of 1 if they are 00 or 0 if they are not in each row.


I tried:



d$last1<-lapply(d$E_numdir,
function(w) lapply(strsplit(w,","),
function(z) {
if(str_sub(z, -2, -1)==00){1}
else {0}}))


But I get an error:



Error in if (str_sub(z, -2, -1) == 0) { : argument is of length zero



The desired result would be



dir E_numdir last2 xx
1 a 1 1 0
2 PJE INDEPENDENCIA 96 5 96, 5 96, 5 0,0
3 PJE INDEPENDENCIA 96 5 96, 5 96, 5 0,0
4 B 34 VIVIENDA RECRE 53 00 34, 53, 00 34, 53, 00 0,0,1
5 SARMIENTO CASA DE GO 613 613 13 0
6 ROCA 958 00 o 958, 00 58, 00 1,0


How can evaluate each element of each row with if function and store it in the same row?


Aucun commentaire:

Enregistrer un commentaire