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.
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
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.
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 ...