0

I have a dataframe with some thousands of entries each, I've different province names. I need to substitute these names (which are repeated) for others.

Quick example, I have this column (I don't know how to show it like a column here):

**Provincia**   Álava Álava Albacete Albacete Albacete ... Zaragoza Zaragoza

And I need to change for:

**Provincia**
ALAVA_1
ALAVA_1
ALBACETE_1
ALBACETE_1
ALBACETE_1
...
ZGZ_1
ZGZ_1

I've tried the following:

old_names <- unique (df$Provincias)

Álava Albacete ... Zaragoza

new_names <- c ("ALAVA_1", "ALBACETE_1",... "ZGZ_1")

df$Provincias <- gsub (old_names, new_names, df$Provincias)

ALAVA_1 ALAVA_1 Albacete Albacete Albacete ... Zaragoza

1: In gsub (new_names, old_names, df$Provincias) : argument 'pattern' has length > 1 and only the first element will be used

2: In gsub (new_names, old_names, df$Provincias) : argument 'replacement' has length > 1 and only the first element will be used

So it only changes the first term (Álava for ALAVA_1) all the times, but it doesn't change the rest of Provincias

Of course I know that I can do this one by one, for the 52 provinces, but then I have more dataframes to do the same... I'm sure there's a better way.

closed as off-topic by Simbamangu, whyzar, BERA, ArMoraer, MaryBeth Feb 26 '18 at 15:46

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions relating to general IT or with no clear GIS component, are off-topic here but can be researched/asked at Stack Overflow (software development), Super User (computing hardware and software), Database Administrators (relational databases) and other SE sites" – Simbamangu, whyzar, BERA, ArMoraer, MaryBeth
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Welcome to GIS SE. Please edit your question to include how this has a spatial component relevant to GIS SE. – Aaron Mar 1 '18 at 15:51
1

Shamelessly stealing this from Map Values and Replace, you can do it like this:

df <- as.data.frame(c("Alava", "Alava", 
            "Albacete","Albacete", "Albacete", 
            "Zaragoza", "Zaragoza"))
colnames(df) <- c("Provincias")
newnames <- c("ALAVA_1", "ALBACETE_1", "ZGZ_1")
oldnames <- unique(df$Provincias)
# create mapping (assuming "newnames" are in the same order as "oldnames"; 
# you can achieve this by sorting the vectors, if necessary)
map <- setNames(newnames, oldnames)
# apply mapping (1 means that you are mapping the first column)
new_df <- as.data.frame(apply(df, 1, function(x) map[as.character(x)]))
colnames(new_df) <- colnames(df)
# check result
df
new_df
  • it seems that it works, i had to change some details because my dataframe has 20k lines and 9 columns, but the "new_df" line is what I needed, cool (and yes, newnames are in same order as oldnames, so perfect!) – manup Feb 26 '18 at 13:59
  • @manup - Great to hear. In that case, please accept the answer to close the question and to indicate to others that this is the solution – s6hebern Feb 26 '18 at 14:07

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