Renaming column values

Hi,

Sometimes the easiest things seem more complex that they should. I am looking to rename column values, not labels.

My data is like this

sample1 <- matrix(c("US.","EU.","JA.","US.","EU.","JA."),ncol=1,byrow=TRUE)
colnames(sample1) <- c("Country")
rownames(sample1) <- c("A","B","C","D", "E","F")
sample1 <- as.table(sample1)
sample1

  Country
A US.    
B EU.    
C JA.    
D US.    
E EU.    
F JA.    

and I want to rename the country acronyms to be like this

sample2 <- matrix(c("USA","EUR","JAP","USA","EUR","JAP"),ncol=1,byrow=TRUE)
colnames(sample2) <- c("Country")
rownames(sample2) <- c("A","B","C","D", "E","F")
sample2 <- as.table(sample2)
sample2

Country
A USA    
B EUR    
C JAP    
D USA    
E EUR    
F JAP    

I have tried the below methods with no success:
rename.values(x, fish="newfish")
and
sample1$Country[sample1$Country == "US."] <-"USA"

In my real data, the format is factor, and I have also tried converting to character to rename.

Appreciate any help.

Thanks,
Ben

Here is one method.

sample1 <- matrix(factor(c("US.","EU.","JA.","US.","EU.","JA.")),ncol=1,byrow=TRUE)
colnames(sample1) <- c("Country")
rownames(sample1) <- c("A","B","C","D", "E","F")
sample1 <- as.table(sample1)
sample1
#>   Country
#> A US.    
#> B EU.    
#> C JA.    
#> D US.    
#> E EU.    
#> F JA.

RENAME <- c(US.="USA",EU.="EUR",JA.="JAP")

sample1[,"Country"] <- RENAME[sample1[,"Country"]]
sample1
#>   Country
#> A USA    
#> B EUR    
#> C JAP    
#> D USA    
#> E EUR    
#> F JAP

Created on 2021-01-25 by the reprex package (v0.3.0)

This seems to work, with mutate() and case_when()

library(tidyverse)

sample1 <- matrix(c("US.","EU.","JA.","US.","EU.","JA."),ncol=1,byrow=TRUE)
colnames(sample1) <- c("Country")
rownames(sample1) <- c("A","B","C","D", "E","F")
sample1 <- as.table(sample1)
sample1
#>   Country
#> A US.    
#> B EU.    
#> C JA.    
#> D US.    
#> E EU.    
#> F JA.

sample1 %>% 
  tibble() %>% 
  purrr::set_names("country") %>% 
  mutate(country = case_when(
    country == "US." ~ "USA",
    country == "EU." ~ "EUR",
    country == "JA." ~ "JAP"
  ))
#> # A tibble: 6 x 1
#>   country
#>   <chr>  
#> 1 USA    
#> 2 EUR    
#> 3 JAP    
#> 4 USA    
#> 5 EUR    
#> 6 JAP

Created on 2021-01-25 by the reprex package (v0.3.0)

1 Like

Thank you for this, when I try to apply this to my data, I get the error:
nm must be NULL or a character vector the same length as x

Please post the result of

dput(head(sample1,10))

where sample1 should be replaced with the name of your actual data.

Hi FJCC,

I am not 100% but I was performing some other manipulation of the data, and when I adjusted your code to RENAME <- c(EU.="EUR", JA.="JAP", US.="USA") this has worked and replaced all the values in the right orders.

I understand it has something to do with this, but need to explore further:
$ Country : Factor w/ 3 levels " EU."," JA.",..: 3 1 3 3 3 3 3 3 1 2 ...

Thanks,
Ben

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Thanks you for this, when I apply this to my data, it is renaming the values, but not in the original rows. Strangely where the value was US. it is now being displayed a JAP, EU. is USA, and JA. is EUR.