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)
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 (.).
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.
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:
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:
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
Here is one approach
library(stringr)
names(df)[-1] = str_sub(names(df)[-1], 2)
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]))
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)))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With