library(dplyr) df %>% mutate(across(ends_with("1"), ~ .x + get(gsub("(.*)1$", # <- search for pattern ending with 1 "\\12", # <- replace with everything \\1 and add 2 cur_column() # <- apply this to current column name ) ), .names = "sum_{gsub...
I'm trying to create a new column in my data frame that is based off another column having a certain unique value. I have successfully done this by using the case_when argument inside a mutate. However, I would like to use a str_detect because I have a great de...
vector might contain names that don't exist in the data,# use `any_of()` insteadlookup <- c(lookup, new ="unknown")try(rename(iris, all_of(lookup)))#> Error in all_of(lookup) : Can'trenamecolumns that don't exist.#> ✖ Column `unknown` doesn't exist.rename(iris, any_of(l...
select()anddrop()functions There are two functions for selection, inverse of each other:selectanddrop. Theselectanddropfunctions accept string labels, integer positions, and/or symbolically represented column names (X.column). They also accept symbolic "selection filter" functions, which will be cov...
These are some things I hope to see or find in dplyr, and may try to build myself if they don’t already exist: 1. Case statements in mutate 2. Creating table indexes 3. type checking of columns, and more informative error messages when un-sensible joins and filters are performed. ...
mutate() Here we use themutate()function to transform the wt variable by multiplying it by 1,000 and then we create a new variable called “good_mpg” which takes on a value of “good” or “bad” depending on if a given row’s MPG value is > 25 or not ...
()operate on a subset of columns. These columuns are selected with either a character vector of columns names, a numeric vector of column positions, or a column specification withselect()semantics generated by the newcolumns()helper. In addition,summarise_if()andmutate_if()take a predicate ...
# Create a time_on_market column using the difference of today’s year and the year_listed airbnb_listings %>% mutate(time_on_market = 2022 - year_listed) # Create a full_address column by combining city and country airbnb_listings %>% transmute(full_address = paste(city, country)) ...
I would like to modify column attributes inside of dplyr::mutate(). Here is some example data: library(dplyr,warn.conflicts=FALSE) v1<-tibble(id=1:5,visit=1,x=1:5,y=c(0,0,0,1,1)) The end result I would like to get is the same result I would get from: ...
df%>%mutate(across(where(is.character),~get_labels(.,df_labels))) the result is anNAin the affected columns. Apparently, the problem is withdeparse(substitute(.)), which does not capture the column names. Unfortunately, looking at similar questions such asthis onedidn't help. ...