0

I am using dplyr package and the function recode while trying to recode a variable, because original authors have many typos. I have managed for instance to do "RKS (UNMIK)"="RKS" or "CU"="CUB" but the other examples from the code don't change. In some cases I even copy-pasted the original typo (i.e. "EGY. EU"), but it didn't work.

These values should be iso3 country codes, fyi.

What I also noticed in this case is that somehow looks like the "(" in "CHE" is matched by the ")" in "NOR". Does anyone know a better way to do it? Thank you in advance!

data<-data%>%
   mutate(parties=recode(parties,
                    " RKS (UNMIK)"="RKS",
                    "(CHE"="CHE",
                    "EGY. EU"="EGY,EU",
                    "BRA. PRY"="BRA-PRY",
                    "CU"="CUB",
                    "NOR)"="NOR",
                    "VNM)KOR"="VNM,KOR"))
r2evans
  • 141,215
  • 6
  • 77
  • 149
AleksP
  • 1
  • 1
  • 2
    Welcome to SO, AleksP! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (please be explicit about non-base packages), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jun 29 '22 at 14:45

1 Answers1

0

I created a sample dataset (df) and labeled the columns according to what you listed in your question. After that I used the rename() command from dplyr to create a new object called "new_data".

The values on the left of the '=' represent the new names, and the values on the right represent the current names.

new_data <- df %>% 
  rename("RKS"     = " RKS (UNMIK)",
         "CHE"     = "(CHE",
         "EGY, EU" = "EGY. EU",
         "BRA-PRY" = "BRA. PRY",
         "CUB"     = "CU",
         "NOR"     = "NOR)",
         "VNM,KOR" = "VNM)KOR")

Hope this is helpful!

j_panda
  • 11
  • 3
  • Thank you very much! It looks very good, I have to yet it try it out. Unfortunately I don't have enough reputation to vote your answer, hope someone else would. – AleksP Jul 05 '22 at 08:37