Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chop off the first letter of every variable name [duplicate]

Tags:

r

dplyr

tidyverse

I have some data that looks like this:

    country agdp apop
1        US  100  100
2 Australia   50   50

The variable names are agdp and apop, but I would like them to be gdp and pop. My real data has many, many variables that all need that transformation.

And this is what my desired outcome:

 country gdp pop
1        US  100  100
2 Australia   50   50

Reproducible code below:

df <- data.frame(stringsAsFactors=FALSE,
     country = c("US", "Australia"),
        agdp = c(100, 50),
        apop = c(100, 50)

desired_df <- data.frame(stringsAsFactors=FALSE,
     country = c("US", "Australia"),
        gdp = c(100, 50),
        pop = c(100, 50)
like image 727
Vu Anh Huynh Avatar asked Jun 11 '19 06:06

Vu Anh Huynh


People also ask

What are the rules for variable names?

The following rules apply to variable names: Each variable name must be unique; duplication is not allowed. Variable names can be up to 64 bytes long, and the first character must be a letter or one of the characters @, #, or $. Subsequent characters can be any combination of letters, numbers, nonpunctuation characters, and a period (.).

How do you change the first letter in a string variable?

Replace the “1” with the letter you want to replace. (1 for first letter, 2 for second letter, 3 for third letter, and so on.) Put the letter you want to change it into in the rectangle. When you click the block, it should change the letter in your variable.

How to remove leading characters from the left side of string?

To remove leading characters from the left side of a string, you also use the REPLACE or RIGHT and LEN functions, but specify how many characters you want to delete every time: For instance, to remove first 2 characters from the string in A2, the formulas are: To remove first 3 characters, the formulas take this form:

How do I delete the first or last n characters from a string?

To delete the first or last n characters from a string, this is what you need to do: On the Ablebits Data tab, in the Text group, click Remove > Remove by Position. On the add-in's pane, select the target range, specify how many characters to delete, and hit Remove. For example, to remove the first character, we configure the following option:


4 Answers

Using regex we can extract everything other than first character and assign the names.

names(df)[-1] <- sub("^.(.*)$", "\\1", names(df)[-1])

df
#    country gdp pop
#1        US 100 100
#2 Australia  50  50
like image 58
Ronak Shah Avatar answered Oct 26 '22 10:10

Ronak Shah


Here is one approach

library(stringr)

names(df)[-1] = str_sub(names(df)[-1], 2)
like image 40
Theo Avatar answered Oct 26 '22 10:10

Theo


One dplyr possibility could be:

df %>%
 rename_at(2:length(.), list(~ substr(., 2, nchar(.))))

    country gdp pop
1        US 100 100
2 Australia  50  50

The same with base R:

names(df)[-1] <- substr(names(df)[-1], 2, nchar(names(df)[-1]))
like image 3
tmfmnk Avatar answered Oct 26 '22 09:10

tmfmnk


One could also do:

Purely base(can use setdiff or %in% to "automate" selection.):

sapply(names(df), function(x) ifelse(x=="country",x,substring(x,2,nchar(x))))

Less elegant with tidyverse since rename_at has been shown:

names(df)<-unlist(names(df) %>% 
  map(.,function(x) ifelse(x=="country",x,substring(x,2,nchar(x)))))
like image 1
NelsonGon Avatar answered Oct 26 '22 09:10

NelsonGon