Tidy evaluation when renaming columns in dplyr - r

I'm trying to dynamically rename a single column in a dataframe using rename() with the tidy evaluation syntax released in dplyr v0.7.0.
From the ?rename help page, I found the following example to rename 2 columns.
library(dplyr)
vars <- c(var1 = "cyl", var2 ="am")
rename(mtcars, !!vars) %>% head(1)
> mpg var1 disp hp drat wt qsec vs var2 gear carb
> Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
However, I've noticed I cannot use this same syntax to rename a single column.
vars <- c(var1 = "cyl")
rename(mtcars, !!vars) %>% head(1)
> Error: All arguments must be named
Yet, when I rename the same column twice, it works.
vars <- c(var1 = "cyl", var1 = "cyl")
rename(mtcars, !!vars) %>% head(1)
> mpg var1 disp hp drat wt qsec vs am gear carb
> Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
Why is this happening? What is the correct syntax?

Use the !!! for evaluation
rename(mtcars, !!!vars) %>%
head(1)
# mpg var1 disp hp drat wt qsec vs am gear carb
#Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4

Related

passing column names in functions as strings and evaluate with mutate

I see gobs of posts about passing column names as strings to function but none of them consider this use case. All the methods I see don't work. Here is one. Please compare the what_I_want column to the what_I_get column below. I want the value of items in the column, not the column name, of course. Thanks.
library(dplyr)
Fun <- function(df,column) {
df %>%
mutate(what_I_want = cyl) %>%
# current best practice? Doen't work in this case.
mutate(what_I_get := {{column}})
}
mtcars[1:2,1:3] %>% Fun("cyl")
#> mpg cyl disp what_I_want what_I_get
#> Mazda RX4 21 6 160 6 cyl
#> Mazda RX4 Wag 21 6 160 6 cyl
Created on 2022-11-07 with reprex v2.0.2
Just add get
Fun <- function(df,column) {
df %>%
mutate(what_I_want = get(column) )
}
mtcars[1:2,1:3] %>% Fun("cyl")
mpg cyl disp what_I_want
Mazda RX4 21 6 160 6
Mazda RX4 Wag 21 6 160 6
We may use ensym which can take both quoted as well as unquoted column name
Fun <- function(df,column) {
df %>%
mutate(what_I_want = !! rlang::ensym(column))
}
-testing
> mtcars[1:2,1:3] %>% Fun("cyl")
mpg cyl disp what_I_want
Mazda RX4 21 6 160 6
Mazda RX4 Wag 21 6 160 6
> mtcars[1:2,1:3] %>% Fun(cyl)
mpg cyl disp what_I_want
Mazda RX4 21 6 160 6
Mazda RX4 Wag 21 6 160 6
Using the .data pronoun you could do:
library(dplyr)
Fun <- function(df,column) {
df %>%
mutate(what_I_get = .data[[column]])
}
mtcars[1:2,1:3] %>%
Fun("cyl")
#> mpg cyl disp what_I_get
#> Mazda RX4 21 6 160 6
#> Mazda RX4 Wag 21 6 160 6
For more on the .data pronoun see Data mask programming patterns.

Dplyr: Conditionally rename multiple variables with regex by name

I need to rename multiple variables using a replacement dataframe. This replacement dataframe also includes regex. I would like to use a similar solution proposed here, .e.g
df %>% rename_with(~ newnames, all_of(oldnames))
MWE:
df <- mtcars[, 1:5]
# works without regex
replace_df_1 <- tibble::tibble(
old = df %>% colnames(),
new = df %>% colnames() %>% toupper()
)
df %>% rename_with(~ replace_df_1$new, all_of(replace_df_1$old))
# with regex
replace_df_2 <- tibble::tibble(
old = c("^m", "cyl101|cyl", "disp", "hp", "drat"),
new = df %>% colnames() %>% toupper()
)
old new
<chr> <chr>
1 ^m MPG
2 cyl101|cyl CYL
3 disp DISP
4 hp HP
5 drat DRAT
# does not work
df %>% rename_with(~ replace_df_2$new, all_of(replace_df_2$old))
df %>% rename_with(~ matches(replace_df_2$new), all_of(replace_df_2$old))
EDIT 1:
The solution of #Mael works in general, but there seems to be index issue, e.g. consider the following example
replace_df_2 <- tibble::tibble(
old = c("xxxx", "cyl101|cyl", "yyy", "xxx", "yyy"),
new = mtcars[,1:5] %>% colnames() %>% toupper()
)
mtcars[, 1:5] %>%
rename_with(~ replace_df_2$new, matches(replace_df_2$old))
Results in
mpg MPG disp hp drat
<dbl> <dbl> <dbl> <dbl> <dbl>
1 21 6 160 110 3.9
meaning that the rename_with function correctly finds the column, but replaces it with the first item in the replacement column. How can we tell the function to take the respective row where a replacement has been found?
So in this example (edit 1), I only want to substitute the second column with "CYL", the rest should be left untouched. The problem is that the function takes the first replacement (MPG) instead of the second (CYL).
Thank you for any hints!
matches should be on the regex-y column:
df %>%
rename_with(~ replace_df_2$new, matches(replace_df_2$old))
MPG CYL DISP HP DRAT
Mazda RX4 21.0 6 160.0 110 3.90
Mazda RX4 Wag 21.0 6 160.0 110 3.90
Datsun 710 22.8 4 108.0 93 3.85
Hornet 4 Drive 21.4 6 258.0 110 3.08
Hornet Sportabout 18.7 8 360.0 175 3.15
Valiant 18.1 6 225.0 105 2.76
#...
If the task is simply to set all col names to upper-case, then this works:
sub("^(.+)$", "\\U\\1", colnames(df), perl = TRUE)
[1] "MPG" "CYL" "DISP" "HP" "DRAT"
In dplyr:
df %>%
rename_with( ~sub("^(.+)$", "\\U\\1", colnames(df), perl = TRUE))
I found a solution using the idea of non standard evaluation from this question and #Maƫl's answer.
Using map_lgl we create a logical vector that returns TRUE if the column in replace_df_2$old can be found inside the dataframe df. Then we pass this logical vector to replace_df_2$new to get the correct replacement.
df <- mtcars[, 1:5]
df %>%
rename_with(.fn = ~replace_df_2$new[map_lgl(replace_df_2$old,~ any(str_detect(., names(df))))],
.cols = matches(replace_df_2$old))
Result:
mpg CYL disp hp drat
Mazda RX4 21.0 6 160.0 110 3.90

Use pipes in R to set data

Is it possible to use the pipe Operator in R (not to get) but to set data?
Lets say i want to modify the first row of mtcars dataset and set the value of qsec to 99.
Traditional way:
mtcars[1, 7] <- 99
Is that also possible using the pipe Operator?
mtcars %>% filter(qsec == 16.46) %>% select(qsec) <- 99
If we are in a state where the chain is absolute necessary or curious to know whether <- can be applied in a chain
library(magrittr)
mtcars %>%
`[<-`(1, 7, 99) %>%
head(2)
# mpg cyl disp hp drat wt qsec vs am gear carb
#Mazda RX4 21 6 160 110 3.9 2.620 99.00 0 1 4 4
#Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
Also, inset (from the comments) is an alias for [<-
mtcars %>%
inset(1, 7, 99) %>%
head(2)

Subset error when using a loop

I have been using this formula:
varlist <- c("A", "B")
for(i in c(1:2)) {
print(varlist[i])
print(summary(svyglm(as.formula(paste0(varlist[i], "~YEAR + REGION")),
design = subset(FEI.w, varlist[i] != "U"),
family = quasibinomial)))
}
I have more variables than just A and B, but I want to do a glm in the survey package using A and B as my dependent variable.
The problem I am running into is that when I subset the data to exclude unknown values in A and B, R doesn't do it and includes the whole data frame.
Any pointers as to why this is happening and how to fix this would be very much appreciated!
subset() uses non-standard evaluation, which means it takes the column names as unquoted variables, e.g.
subset(mtcars, mpg == 21)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
vs
subset(mtcars, "mpg" == 21)
#> [1] mpg cyl disp hp drat wt qsec vs am gear carb
#> <0 rows> (or 0-length row.names)
Your varlist[i] != "U" compares the literal strings "A" and "U" and finds that they aren't equal.
You might be able to get around this with
eval(parse(text = varlist[i])) != "U"
i.e.
subset(mtcars, eval(parse(text="mpg")) == 21)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
but the old adage goes that if you're using eval(parse( then something has probably gone wrong.
svyglm has a subset parameter so you don't need to call subset on the design object. You should do the subsetting like this:
library(survey)
data(api)
dstrat<-svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
rstrat<-as.svrepdesign(dstrat)
for (type in unique(apistrat$stype)) {
print(summary(svyglm(api00~ell+meals+mobility,
design = rstrat,
subset = apistrat$stype==type)))
}

Re-assembling a dataframe after a split [duplicate]

This question already has answers here:
Grouping functions (tapply, by, aggregate) and the *apply family
(10 answers)
Closed 6 years ago.
I have trouble applying a split to a data.frame and then assembling some aggregated results back into a different data.frame. I tried using the 'unsplit' function but I can't figure out how to use it properly to get the desired result. Let me demonstrate on the common 'mtcars' data: Let's say that my ultimate result is to get a data frame with two variables: cyl (cylinders) and mean_mpg (mean over mpg for group of cars sharing the same count of cylinders).
So the initial split goes like this:
spl <- split(mtcars, mtcars$cyl)
The result of which looks something like this:
$`4`
mpg cyl disp hp drat wt qsec vs am gear carb
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
...
$`6`
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
...
$`8`
mpg cyl disp hp drat wt qsec vs am gear carb
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
...
Now I want to do something along the lines of:
df <- as.data.frame(lapply(spl, function(x) mean(x$mpg)), col.names=c("cyl", "mean_mpg"))
However, doing the above results in:
X4 X6 X8
1 26.66364 19.74286 15.1
While I'd want the df to be like this:
cyl mean_mpg
1 4 26.66364
2 6 19.74286
3 8 15.10000
Thanks, J.
If you are only interested in reassembling a split then look at (2), (4) and (4a) but if the actual underlying question is really about the way to perform aggregations over groups then they all may be of interest:
1) aggregate Normally one uses aggregate as already mentioned in the comments. Simplifying #alistaire's code slightly:
aggregate(mpg ~ cyl, mtcars, mean)
2) split/lapply/do.call Also #rawr has given a split/lapply/do.call solution in the comments which we can also simplify slightly:
spl <- split(mtcars, mtcars$cyl)
do.call("rbind", lapply(spl, with, data.frame(cyl = cyl[1], mpg = mean(mpg))))
3) do.call/by The last one could alternately be rewritten in terms of by:
do.call("rbind", by(mtcars, mtcars$cyl, with, data.frame(cyl = cyl[1], mpg = mean(mpg))))
4) split/lapply/unsplit Another possibility is to use split and unsplit:
spl <- split(mtcars, mtcars$cyl)
L <- lapply(spl, with, data.frame(cyl = cyl[1], mpg = mean(mpg), row.names = cyl[1]))
unsplit(L, sapply(L, "[[", "cyl"))
4a) or if row names are sufficient:
spl <- split(mtcars, mtcars$cyl)
L <- lapply(spl, with, data.frame(mpg = mean(mpg), row.names = cyl[1]))
unsplit(L, sapply(L, rownames))
The above do not use any packages but there are also many packages that can do aggregations including dplyr, data.table and sqldf:
5) dplyr
library(dplyr)
mtcars %>%
group_by(cyl) %>%
summarize(mpg = mean(mpg)) %>%
ungroup()
6) data.table
library(data.table)
as.data.table(mtcars)[, list(mpg = mean(mpg)), by = "cyl"]
7) sqldf
library(sqldf)
sqldf("select cyl, avg(mpg) mpg from mtcars group by cyl")

Resources