Issue running across on dplyr on R 1.4.1103 - r

When running this simple code :
freq_table <- ques %>%
select(across(starts_with("Q36_"))) %>%
table()
I get the following error message :
across()` must only be used inside dplyr verbs
I have restarted R and reloaded dplyr but the problem remains. Any clues why?
Thanks!

Related

Why wont replace_na actually replace the missing values using dplyr and piping?

I have been struggling a lot recently with the replace_na() function when cleaning my data. I have two complementary variables and I want to use one variable (varname2) to supply the missing values for the other (varname1). I've been trying the following:
df %>%
replace_na(varname = varname2)
In response I keep getting the error:
Did you misspecify an argument?
Run `rlang::last_error()` to see where the error occurred.
> df <- df %>%
+ replace_na(varname1= varname2)
Error: 1 components of `...` were not used.
We detected these problematic arguments:
* `varname1`
Suggestions for an efficient way to fix this?
I found a blog response elsewhere in which Hadley himself said they wanted to move away from replace_na() toward a more SQL adjacent command coalesce(). The solution involves both across() and coalesce().
Here's an example of what I just did in my work:
df %>%
mutate(across(varname1, coalesce, varname2))
It seems to have worked like a charm.

R table1 within furniture package

I am trying to create a latex based table1 using the following variables.
However, whenever I run the code, I get the error pasted below. I have attempted several times to load packages that include the magrittr pipe (including dplyr, tidyr, magrittr) but the error still appears. Does anyone have any experience with this package?
Code:
library(furniture)
table1(tb_table1,
SEX, AGE, RACE)
Error: Error in selecting(data.frame(.data, stringsAsFactors = TRUE), ...) %>% :
object 'magrittr_pipe' not found
I had the same error and solved it simply by updating both packages.

Invalid column using dplyr + NSE

The following code works fine on my mac, using CRAN R:
delta_scores <- function(df, data_var) {
# Use Hadley's new non-standard evaluation helpers to compute differences in
# the symbol passed through data_var from Session 1 to 2. Assumes an ID column
# in df that groups units of measurement.
# For the RHS:
quo_data_var <- enquo(data_var)
# For the LHS, we need yet another step (basically a string)
name_data_var <- quo_name(quo_data_var)
df %>% select(ID, Session, !!quo_data_var) %>%
# NSE spread stopped working on my windows machine!
spread(Session, !!quo_data_var) %>%
# Note use of := instead of plain = to support NSE
transmute(ID=ID, !!name_data_var := `2`-`1`)
}
test_df <- data_frame(ID=c(1,2,3,1,2,3),
Session=c(1,1,1,2,2,2),
Measure=c(1,2,3,1,1,4))
delta_scores(test_df, Measure)
But when I run it on Windows, Microsoft R Open 3.4.2, dplyr 0.7.3, I get:
Error: Invalid column specification
NOTE: it's easy enough to fix by replacing spread with spread_('Session', name_data_var). Interestingly, the select call works fine (my real data frames have many columns). I'm concerned about the bigger issue of dplyr's NSE not working in a given environment.
Looking at the debugger stacktrace was daunting enough that I decided to ask for help here first. Any ideas about what's going on or ideas on how to debug this are much appreciated!
This is solved in later versions of tidyr. Confirmed working in tidyr 0.7.2. Support for Hadley's new NSE (non-standard evaluation) system was added in the 0.7 release.

How to use require(googlesheets) properly?

I recently downloaded googlesheets via
devtools::install_github("jennybc/googlesheets")
and experience some difficulties. When running the script as mentioned in
https://github.com/jennybc/googlesheets I get always:
Error: could not find function "%>%"
How can I solve that problem?
Reproducible example:
Download:
devtools::install_github("jennybc/googlesheets")
require(googlesheets)
Data:
gap_key <- "1HT5B8SgkKqHdqHJmn5xiuaC04Ngb7dG9Tv94004vezA"
copy_ss(key = gap_key, to = "Gapminder")
gap <- register_ss("Gapminder")
Error occurs:
oceania_csv <- gap %>% get_via_csv(ws = "Oceania")
Load the dplyr package first, which provides the %>% operator. This is noted here in the README you link to (suppressMessages is optional):
googlesheets is designed for use with the %>% pipe operator and, to a lesser extent, the data-wrangling mentality of dplyr. The examples here use both, but we'll soon develop a vignette that shows usage with plain vanilla R. googlesheets uses dplyr internally but does not require the user to do so.
library("googlesheets")
suppressMessages(library("dplyr"))
You can install dplyr with
install.packages("dplyr")
See here for more about the pipe operator (%>%).

Remove/detach a package in a R session

My question is some packages share the same function name. How can I tell R which package that I want to use this function from?
I tried to load the package that I wanted to use again in the code but it still did not work. My case is the select in MASS and dplyr. I want to use dplyr but the error is always unused argument...
You can use the :: operator:
iris %>%
head(n = 3) %>%
dplyr::select(Sepal.Length)
See here for details.
Or detach MASS ala this post.

Resources