Home > Mobile >  replace row names with defined vector in R
replace row names with defined vector in R

Time:12-22

Is there a way that the row names can be substituted based on predefined vector in R, something like:

rownames(GV) <- c(beta1='Age', beta10='Female Gender')

enter image description here

CodePudding user response:

Or maybe case_when() will be easier for you:

library(dplyr)

df <- data.frame(a = c(1, 2, 3))

rownames(df)
#> [1] "1" "2" "3"

rownames(df) <- case_when(rownames(df) == "1" ~ "one",
                          rownames(df) == "2" ~ "two",
                          TRUE ~ rownames(df))

rownames(df)
#> [1] "one" "two" "3"

You specify new value for each contidion and the value for all rest cases (where is TRUE ~ rownames(df) line) - for the rest cases I'm leaving the previous row names above.

CodePudding user response:

We could do the following:

rownames(mtcars)[which(rownames(mtcars) == "Datsun 710")] <- "My Rowname" 
head(mtcars)

#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> My Rowname        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

If we want to rename more rownames we can use %in%, but as @gss mentions in the comments, this comes with a caveat: not matter the order of the names in the character vector succeeding %in% the names will be replaced in the order they appear in rownames(). Compare the following two calls:

rownames(mtcars)[which(rownames(mtcars) %in% c("Datsun 710", "Mazda RX4 Wag"))] <- c("My Rowname1","My Rowname2")
head(mtcars)
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> My Rowname1       21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> My Rowname2       22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Which has the same result as:

rownames(mtcars)[which(rownames(mtcars) %in% c("Mazda RX4 Wag", "Datsun 710"))] <- c("My Rowname1","My Rowname2")
head(mtcars)

#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> My Rowname1       21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> My Rowname2       22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Created on 2021-12-21 by the reprex package (v2.0.1)

CodePudding user response:

If you want to rename all the rows, and you have an array of the desired new names in order:

example <- head(mtcars, 3)
mynewnames <- c("First", "Second", "Third")
rownames(example) <- mynewnames
example
#>         mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> First  21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Second 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Third  22.8   4  108  93 3.85 2.320 18.61  1  1    4    1

If you want to rename all the rows, and you have a named array (not necessarily in the correct order):

example <- head(mtcars, 3)
mynewnames <- c("Datsun 710" = "Datsun", "Mazda RX4" = "Mazda", "Mazda RX4 Wag" = "Also Mazda")
rownames(example) <- mynewnames[rownames(example)]
example
#>             mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda      21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Also Mazda 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun     22.8   4  108  93 3.85 2.320 18.61  1  1    4    1

If you want to rename only some rows, and have a named array (an ordered array makes no sense in this context):

example <- head(mtcars, 3)
mynewnames <- c("Mazda RX4" = "This Mazda", "Mazda RX4 Wag" = "That Mazda")
rownames(example)[rownames(example) %in% names(mynewnames)] <-
  mynewnames[rownames(example)[rownames(example) %in% names(mynewnames)]]
example
#>             mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> This Mazda 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> That Mazda 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1

This is a bit unwieldy; if you are only replacing one or two row names then @TimTeaFan's first suggestion is probably easier.

CodePudding user response:

Most safe way and as OP prefers with a predefined named vector is taking the current rownames, replace those who are defined and set the rownames again. this does not fail on an incomplete vector, if it cannot be replaced it stays as it was before.

The advantage of this solution is to prevent the error below if your rename vector is incomplete.

Error in `.rowNamesDF<-`(x, value = value) : 
  missing values in 'row.names' are not allowed

solution

library(stringr) # used for str_replace_all()

df <- data.frame(
  x = rep(1:5),
  y = rep(11:15),
  row.names = LETTERS[1:5]
)

df

#   x  y
# A 1 11
# B 2 12
# C 3 13
# D 4 14
# E 5 15

change <- c("A" = "a", "C" = "c")

row.names(df) <- str_replace_all(row.names(df), change)

df

#   x  y
# a 1 11
# B 2 12
# c 3 13
# D 4 14
# E 5 15
  • Related