Simple data frame rowname trim question

I have an object with a dataframe in it that has rownames as follows:


How do I get rid of the _ at the beggining of each rowname?


Hi @cook675,

Is it a variable or truly a rowname? It is a variable, depending on the name of the variable, try this:


data <- tibble(x = c("_ABC", "_DEF", "_GHI"))

data %>%
  mutate(x = str_replace(x, "_", ""))

I can be more helpful if you show the actual structure of the data.

Thanks for the reply, they are actual rownames and not variables, however, I was trying to generalize in the problem. These are not the actual rownames; and there are 17,000 of them and they all begin with "_" ! So how to remove the underscore from them all across the set?

The solution would be almost the same

df <- data.frame(x = 1:3, 
                 row.names = c("_ABC", "_DEF", "_GHI")
#>      x
#> _ABC 1
#> _DEF 2
#> _GHI 3

rownames(df) <- gsub(pattern = "_", replacement = "", x = rownames(df))
#>     x
#> ABC 1
#> DEF 2
#> GHI 3

Created on 2019-11-13 by the reprex package (v0.3.0.9000)
Note: Please make your questions providing a proper REPRoducible EXample (reprex) like the one above.

1 Like

Thanks both of you I really appreciate it!

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