Rename Columns Using 'Starts_With()' Where New Prefix Is a String

Rename columns using `starts_with()` where new prefix is a string

The option would be to use rename_at

mtcars %>% 
rename_at(vars(starts_with('m')), funs(paste0('prefix', .)))

For changing an old name, use sub

change2 <- function(df, oldpref, newpref) {
df %>%
rename_at(vars(starts_with(oldpref)), funs(sub(oldpref, newpref, .)))

}

change2(mtcars, "m", "newprefix") %>%
names
#[1] "newprefixpg" "cyl" "disp" "hp" "drat"
#[6] "wt" "qsec" "vs" "am" "gear"
#[11] "carb"

rename_if() together with starts_with() to prefix certain columns

rename_if expects a logical vector as predicate function. starts_with selects variables based on their name. Use base startsWith instead which returns a logical vector based on prefix

library(dplyr)
df1 %>% rename_if(startsWith(names(.), "A"), ~paste0("df1_", .))

# df1_Artist df1_Album Year
#1 Beatles Sgt. Pepper's 1967
#2 Rolling Stones Sticky Fingers 1971

Or if you want to stay in tidyverse you can also use str_detect

df1 %>% rename_if(stringr::str_detect(names(.), "^A"), ~paste0("df1_", .))

To use starts_with we can use rename_at which has vars argument.

df1 %>% rename_at(vars(starts_with("A")), ~paste0("df1_", .))

How to rename only certain columns names in a data.frame by adding a prefix?

In base R, we can use startsWith to identify column names which start with a prefix

inds <- startsWith(names(df), "rename")
#Or grep
#inds <- grep("^rename", names(df))
names(df)[inds] <- paste0("finished_", names(df)[inds])

df
# do_not_rename finished_rename1 finished_rename2
#1 1 1 1
#2 2 2 2
#3 3 3 3

Rename columns that start with prefix and delete periods using gsub?

rename_with can take a select statement, so we can use starts_with to apply the function just to those columns that start with Q.

library(dplyr)

df %>%
rename_with(.fn = ~ tolower(gsub(".", "", .x, fixed = TRUE)), .col = starts_with("Q"))

#[1] A.1 q11 q12
#<0 rows> (or 0-length row.names)

Or with stringr:

library(stringr)

df %>%
rename_with(.fn = ~ str_to_lower(str_replace_all(.x, "\\.", "")), .col = starts_with("Q"))

Rename all columns except id column by adding a prefix using dplyr

library(dplyr)

df %>%
dplyr::rename_with(~ paste0("source_", .), -id)

The third argument to rename_with is .cols, where you can use tidyselect syntax to select the columns. Here -id excludes this column.


Per the comments the . syntax is a cleaner/simpler style than an writing an anonymous function, but you could accomplish this equivalently as:

df %>% 
dplyr::rename_with(function(x) paste0("source_", x), -id)

Error when using dplyr rename_at and stringr str_replace together to rename columns

You need to use the ~ notation when using a custom function.

library(dplyr)

test %>%
dplyr::rename_at(dplyr::vars(starts_with('pre')),
~stringr::str_replace(., pattern = 'pre', replacement ='PRE'))

Also rename_at is now replaced with rename_with.

test %>% 
dplyr::rename_with(~stringr::str_replace(.,pattern = 'pre',replacement ='PRE'),
dplyr::starts_with('pre'))

# A tibble: 2 x 3
# PRE_a PRE_b c
# <dbl> <dbl> <dbl>
#1 1 2 3
#2 4 5 6

SQL select join: is it possible to prefix all columns as 'prefix.*'?

I see two possible situations here. First, you want to know if there is a SQL standard for this, that you can use in general regardless of the database. No, there is not. Second, you want to know with regard to a specific dbms product. Then you need to identify it. But I imagine the most likely answer is that you'll get back something like "a.id, b.id" since that's how you'd need to identify the columns in your SQL expression. And the easiest way to find out what the default is, is just to submit such a query and see what you get back. If you want to specify what prefix comes before the dot, you can use "SELECT * FROM a AS my_alias", for instance.

How to remove a class that starts with...?

.removeClass() accepts a function for removing classes and accepts index and old CSS value:

A function returning one or more space-separated class names to be removed. Receives the index position of the element in the set and the old class value as arguments.

You can remove the classname that starts with required name and keep the existing using:

$("div[class*='ativo']").removeClass (function (index, css) {
return (css.match (/(^|\s)ativo\S+/g) || []).join(' ');
});

Working Demo



Related Topics



Leave a reply



Submit