1

I'm sorry for my english :)

I need to detect if there is a special character in an address (variable "no_trans", 1 : if there is one or several special character, else 0) and in the next column ("carac_no_trans") I need to put the list of the special character, so if there is several special character I need to see all in this column.

My code (liste_carac is the list of non-special character. I need to detect all characters not included in this list) :

new <- data_adresse %>% 
  group_by(e_street_name) %>% 
  mutate(
    no_trans = ifelse(
      length(
        setdiff(
          unlist(strsplit(e_street_name,"")),
          liste_carac
        )
      ) >= 1,
      1,
      0
    ),
    carac_no_trans = ifelse(
      length(
        as.list(
          setdiff(
            unlist(strsplit(e_street_name,"")),
            liste_carac
          )
        )
      ) >= 1,
      as.list(
        setdiff(
          unlist(strsplit(e_street_name,""))
          liste_carac
        ),
      ),
      "None"
    )
  )

But I see just the first special character and not all..

enter image description here

If you can help me :)

Have a good day ! Elise

3
  • 1
    Welcome to StackOverflow! Please read the info about how to give a reproducible example. This will make it much easier for others to help you.
    – JBGruber
    Commented Apr 15, 2020 at 9:04
  • You could use paste to combine all the special characters into one string. Commented Apr 15, 2020 at 9:07
  • I try to replace as.list by paste but I have the same issue @BertilBaron .. Commented Apr 15, 2020 at 9:33

1 Answer 1

0

you can use paste with the parameter collaps = ", " to reduce the list into one single string like this

new <- data_adresse %>% 
  group_by(e_street_name) %>% 
  mutate(
    no_trans = ifelse(
      length(
        setdiff(
          unlist(strsplit(e_street_name,"")),
          liste_carac
        )
      ) >= 1,
      1,
      0
    ),
    carac_no_trans = ifelse(
      length(
        as.list(
          setdiff(
            unlist(strsplit(e_street_name,"")),
            liste_carac
          )
        )
      ) >= 1,
      paste(
        setdiff(
          unlist(strsplit(e_street_name,"")),
          liste_carac
        ), collapse = ", "
      ),
      "None"
    )
  )

Hope this helps!!

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.