Count causing Argument 'x' must be a vector: list - r

I posted a question previously, here and for some reason if I run the code now:
mtcars %>%
filter(gear == 4) %>%
select(vs, am) %>%
pivot_longer(everything()) %>%
count(name, value) %>%
mutate(perc = n/sum(n) * 100)
It is now returning:
Error in count(., name, value) : Argument 'x' must be a vector: list
It was functional just a month ago, so I am baffled as to what is causing this.

Most probably, it is a case of masking of the function with same function from a different package that got accidentally loaded. If we use :: to specify the package, it should work
mtcars %>%
dplyr::filter(gear == 4) %>%
dplyr::select(vs, am) %>%
tidyr::pivot_longer(everything()) %>%
dplyr::count(name, value) %>%
dplyr::mutate(perc = n/sum(n) * 100)
Here, we used the :: in each of the function because select/filter/mutate/count are found in more than one package

Related

Error in View : invalid caption argument - why does R show this error

I wanted to view the transformed dataframe using the flights dataset but R shows an invalid caption argument error
library(dplyr)
library(nycflights13)
view (temp <- flights %>%
group_by(year, month, day) %>%
mutate(r = min_rank(desc(dep_time))) %>%
filter(r %in% range(r)))
Error in View : invalid caption argument
However, this one with the piping operator works fine.
(temp <- flights %>%
group_by(year, month, day) %>%
mutate(r = min_rank(desc(dep_time))) %>%
filter(r %in% range(r))) %>% view()
So does this one (with a capital V)
View (temp <- flights %>%
group_by(year, month, day) %>%
mutate(r = min_rank(desc(dep_time))) %>%
filter(r %in% range(r)))
Even this one (where the transformed dataframe is not assigned to an object works)
view (flights %>%
group_by(year, month, day) %>%
mutate(r = min_rank(desc(dep_time))) %>%
filter(r %in% range(r)))
Could anyone explain what's happening and why the error in the first case and not the other three? Thank you in advance.

Error with mutate_at in dplyr - trying windowed lag by group

This is what I have right now:
final.df <- all.df %>% dplyr::arrange(customer, date) %>%
dplyr::select(year, week, customer, date, ltv_score,
avg_monthly_sales:most_recent_login) %>%
group_by(customer, year) %>%
dplyr::mutate_at(c("date"), list(~lead), n = 1) %>%
data.frame()
I am trying to offset everything by one date within the year/customer to backtest predictions - basically have a rolling input of of each group. I found this snippet elsewhere and modified it for what I need but am getting the following error:
Error: Input must be a vector, not a <formula> object.
Note that all the _at, _all, _if verbs are deprecated in favour of across. For a single column you don't need mutate_at/across.
library(dplyr)
final.df <- all.df %>%
dplyr::arrange(customer, date) %>%
dplyr::select(year, week, customer, date, ltv_score,
avg_monthly_sales:most_recent_login) %>%
group_by(customer, year) %>%
dplyr::mutate(date = lead(date)) %>%
data.frame()

How to reuse parts of long chain of pipe operators in R?

I have a set of chains of pipe operators (%>%) doing different things with different datasets.
For instance:
dataset %>%
mutate(...) %>%
filter(...) %>%
rowwise() %>%
summarise() %>%
etc...
If I want to reuse some parts of these chains, is there a way to do it, without just wrapping it into a function?
For instance (in pseudocode obviously):
subchain <- filter(...) %>%
rowwise() %>%
summarise()
# and then instead of the chain above it would be:
dataset %>%
mutate(...) %>%
subchain() %>%
etc...
Similar in syntax to desired pseudo-code:
library(dplyr)
subchain <- . %>%
filter(mass > mean(mass, na.rm = TRUE)) %>%
select(name, gender, homeworld)
all.equal(
starwars %>%
group_by(gender) %>%
filter(mass > mean(mass, na.rm = TRUE)) %>%
select(name, gender, homeworld),
starwars %>%
group_by(gender) %>%
subchain()
)
Using a dot . as start of a piping sequence. This is in effect close to function wrapping, but this is called a magrittr functional sequence. See ?functions and try magrittr::functions(subchain)

dplyr: How to use select and filter inside functions; (...) not working for arguments

I'm trying to build some functions for creating standard tables from a questionnaire, using dplyr for the data manipulation. This question was very helpful for the group_by function, passing arguments (in this case, the name of the variable I want to use to make the table) to (...), but that seems to break down when trying to pass the same arguments to other dplyr commands, specifically 'select' and 'filter'. The error message I get is '...' used in an incorrect context'.
Does anyone have any ideas on this? Thank you
For the sake of completeness (and any other hints - I'm very new to writing functions), here is the code I would like to use:
myTable <- function(x, ...) {
df <-
x %>%
group_by(Var1, ...) %>%
filter(!is.na(...) & ... != '') %>% # To remove missing values: Not working!
summarise(value = n()) %>%
group_by(Var1) %>%
mutate(Tot = sum(value)) %>%
group_by(Var1, ...) %>%
summarise(num = sum(value), total = sum(Tot), proportion = num/total*100) %>%
select(Var1, ..., proportion) # To select desired columns: Not working!
tab <- dcast(df, Var1 ~ ..., value.var = 'proportion')
tab[is.na(tab)] <- 0
print(tab)
}

Use of other columns as arguments to function in summarize_if()

This works great (see it as a solution for using list() instead of vars() here):
mtcars %>%
group_by(cyl) %>%
summarize_at(vars(disp, hp), list(~weighted.mean(., wt)))
However, in a very similar situation using summarize_if(), it does not work:
mtcars %>%
group_by(cyl) %>%
summarize_if(is.numeric, list(~weighted.mean(., wt)))
Error in weighted.mean.default(., wt) :
'x' and 'w' must have the same length
Why?
I believe this has to do with what you are naming this new variable. This works:
mtcars %>%
group_by(cyl) %>%
summarize_if(is.numeric, list(tmp = ~weighted.mean(., wt)))
See the naming section here and issues that have been noted here for more details.

Resources