magrittr

How to use logical operator (!) with magrittr in R

和自甴很熟 提交于 2021-02-18 20:53:06
问题 I am taking a list of values and trying to find those that are not NA using magrittr. Here is a simple example: data.frame(data = c(1:2, NA, 4:5, NA, 7)) %>% is.na which yields the correct result: data [1,] FALSE [2,] FALSE [3,] TRUE [4,] FALSE [5,] FALSE [6,] TRUE [7,] FALSE When I put the not operator ! in front of is.na , I get an error: data.frame(data = c(1:2, NA, 4:5, NA, 7)) %>% !is.na gives me Error in FUN(left, right) : operations are possible only for numeric, logical or complex

How to use logical operator (!) with magrittr in R

大兔子大兔子 提交于 2021-02-18 20:53:05
问题 I am taking a list of values and trying to find those that are not NA using magrittr. Here is a simple example: data.frame(data = c(1:2, NA, 4:5, NA, 7)) %>% is.na which yields the correct result: data [1,] FALSE [2,] FALSE [3,] TRUE [4,] FALSE [5,] FALSE [6,] TRUE [7,] FALSE When I put the not operator ! in front of is.na , I get an error: data.frame(data = c(1:2, NA, 4:5, NA, 7)) %>% !is.na gives me Error in FUN(left, right) : operations are possible only for numeric, logical or complex

R: can I update the class of a an object in a magrittr pipe?

痞子三分冷 提交于 2021-02-11 15:54:02
问题 I have a piece of code where I update the class of the object. But I have to break the follow of the code to assign the class. Is there an elegant to to assign the class but continue the pipe so I have one pipe all the way to the final result? I suspect there might be something in {purrr}? library(disk.frame) library(dplyr) library(tidyquery) a = nycflights13::airports %>% as.disk.frame class(a) <- c(class(a), "data.frame") a %>% query("SELECT name, lat, lon ORDER BY lat DESC LIMIT 5") 回答1:

Magrittr forward pipe fails to forward values into openXL::addWorksheet - “Error …: First argument must be a Workbook”

强颜欢笑 提交于 2021-02-07 20:27:14
问题 magrittr appears to be failing to pipe 'workbook' class objects into the addWorkbook function from the package openxlsx . ( Never mind why I need to use excel...eugh yuk ) For example, to write the InsectSprays dataset to an excel file in 'base' syntax: library("openxlsx") insect.wb <- createWorkbook() addWorksheet(wb = insect.wb, sheetName = "Insect Spray") writeData(wb = insect.wb, sheet = "Insect Spray", x= InsectSprays) openXL(insect.wb) Opens a temp excel file with the data. So magrittr

What is the difference between the “+” operator in ggplot2 and the “%>%” operator in magrittr?

假装没事ソ 提交于 2021-02-03 06:41:29
问题 What is the difference between the "+" operator in ggplot2 and the "%>%" operator in magrittr? I was told that they are the same, however if we consider the following script. library(magrittr) library(ggplot2) # 1. This works ggplot(data = mtcars, aes(x=wt, y = mpg)) + geom_point() # 2. This works ggplot(data = mtcars) + aes(x=wt, y = mpg) + geom_point() # 3. This works ggplot(data = mtcars) + aes(x=wt, y = mpg) %>% geom_point() # 4. But this doesn't ggplot(data = mtcars) %>% aes(x=wt, y =

What is the difference between the “+” operator in ggplot2 and the “%>%” operator in magrittr?

你离开我真会死。 提交于 2021-02-03 06:41:26
问题 What is the difference between the "+" operator in ggplot2 and the "%>%" operator in magrittr? I was told that they are the same, however if we consider the following script. library(magrittr) library(ggplot2) # 1. This works ggplot(data = mtcars, aes(x=wt, y = mpg)) + geom_point() # 2. This works ggplot(data = mtcars) + aes(x=wt, y = mpg) + geom_point() # 3. This works ggplot(data = mtcars) + aes(x=wt, y = mpg) %>% geom_point() # 4. But this doesn't ggplot(data = mtcars) %>% aes(x=wt, y =

What is the difference between the “+” operator in ggplot2 and the “%>%” operator in magrittr?

江枫思渺然 提交于 2021-02-03 06:39:01
问题 What is the difference between the "+" operator in ggplot2 and the "%>%" operator in magrittr? I was told that they are the same, however if we consider the following script. library(magrittr) library(ggplot2) # 1. This works ggplot(data = mtcars, aes(x=wt, y = mpg)) + geom_point() # 2. This works ggplot(data = mtcars) + aes(x=wt, y = mpg) + geom_point() # 3. This works ggplot(data = mtcars) + aes(x=wt, y = mpg) %>% geom_point() # 4. But this doesn't ggplot(data = mtcars) %>% aes(x=wt, y =

'mutate' to add two columns with a single fn-call in tidyverse in R

Deadly 提交于 2021-01-29 09:21:37
问题 This is an R Version 3.4.4 question A voting function voteOnBase , takes 2 arguments and returns a 2-element list: the WINNER and the VOTE.COUNT . I want to use it to add those two columns to notVotedYet , a tibble. The following code runs correctly. library(tidyverse) withVotes <- notVotedYet %>% group_by(BASE) %>% mutate(WINNER = voteOnBase(BASE, CODES)[[1]], VOTE.COUNT = voteOnBase(BASE, CODES)[[2]]) However, it calls voteOnBase twice on the same inputs. How can I eliminate the extra

Strange behaviour when piping to return() in R function?

♀尐吖头ヾ 提交于 2020-08-07 15:08:10
问题 Under certain circumstances, piping to return() doesn't appear to behave expectedly. To demonstrate, here are 4 cases Suppose we define a function that return s the result of str_replace_all library(stringr) library(dplyr) string <- letters[1:9] %>% paste0(collapse="") funct <- function(string) { return(string %>% str_replace_all(., "ef", "HHH")) } funct(string) # [1] "abcdHHHghi" Now suppose we pipe to return - function works as expected funct <- function(string) { string %>% str_replace_all

R package fails devtools::check, because “could not find function” even though the function is imported in NAMESPACE

生来就可爱ヽ(ⅴ<●) 提交于 2020-07-21 05:25:47
问题 Trying to build my first R package using roxygen2 and devtools . I have added a function that uses %>% and mutate in the @examples section. When I run check() it fails, because it cannot find the function %>% or mutate . Based on this, this, and this I have tried the following: I have #' importFrom magrittr %>% and #' importFrom dplyr mutate in the function's .R file. I also have magrittr and dplyr under Imports: in the DESCRIPTION file. After running document() , my NAMESPACE file contains