magrittr

Get name of dataframe passed through pipe in R

大兔子大兔子 提交于 2019-11-27 02:53:41
问题 I would like to be able to print the name of a dataframe passed through the pipe. Is this possible? I can do. printname <- function(df){ print(paste(substitute(df))) } printname(mtcars) #[1] "mtcars" However, it returns "." when this function is piped using the magrittr pipe. mtcars %>% printname # [1] "." This would be helpful when writing custom error messages of functions used in logged production processes -- it's hard to know where something failed if the only thing in the log is "." It

rollmean with dplyr and magrittr

半城伤御伤魂 提交于 2019-11-27 02:21:04
问题 Given the following data: set.seed(1) data <- data.frame(o=c('a','a','a','a','b','b','b','b','c','c','c','c'), t=c(1,2,3,4,1,2,3,4,1,2,3,4), u=runif(12), v=runif(12)) data o t u v 1 a 1 0.26550866 0.6870228 2 a 2 0.37212390 0.3841037 3 a 3 0.57285336 0.7698414 4 a 4 0.90820779 0.4976992 5 b 1 0.20168193 0.7176185 6 b 2 0.89838968 0.9919061 7 b 3 0.94467527 0.3800352 8 b 4 0.66079779 0.7774452 9 c 1 0.62911404 0.9347052 10 c 2 0.06178627 0.2121425 11 c 3 0.20597457 0.6516738 12 c 4 0.17655675

Should I avoid programming packages with pipe operators?

孤街醉人 提交于 2019-11-27 02:11:50
问题 Are there any objective reasons for why pipe operators from the R package magrittr , such as %>% , should be avoided when I program packages in R? More specifically, I want to know if using pipe operators might cause coding conflicts or (positively or negatively) affect performance. I am looking for specific, concrete examples of such cases. 回答1: Like all advanced functions written in R, %>% carries a lot of overhead, so don't use it in loops (this includes implicit loops, such as the *apply

Using the %>% pipe, and dot (.) notation

ぐ巨炮叔叔 提交于 2019-11-26 22:16:37
When using map on a nested data_frame, I do not understand why the latter two version give an error, how should I use the dot ( . )? library(tidyverse) # dummy data df <- tibble(id = rep(1:10, each = 10), val = runif(100)) df <- nest(df, -id) # works as expected map(df$data, min) df %>% .$data %>% map(., min) # gives an error df %>% map(.$data, min) # Error: Don't know how to index with object of type list at level 1 df %>% map(data, min) The problem isn't map , but rather how the %>% pipe deals with the . . Consider the following examples (remember that / is a two argument function in R):

filter for complete cases in data.frame using dplyr (case-wise deletion)

♀尐吖头ヾ 提交于 2019-11-26 21:46:41
Is it possible to filter a data.frame for complete cases using dplyr? complete.cases with a list of all variables works, of course. But that is a) verbose when there are a lot of variables and b) impossible when the variable names are not known (e.g. in a function that processes any data.frame). library(dplyr) df = data.frame( x1 = c(1,2,3,NA), x2 = c(1,2,NA,5) ) df %.% filter(complete.cases(x1,x2)) Try this: df %>% na.omit or this: df %>% filter(complete.cases(.)) or this: library(tidyr) df %>% drop_na If you want to filter based on one variable's missingness, use a conditional: df %>% filter

R Conditional evaluation when using the pipe operator %>%

随声附和 提交于 2019-11-26 19:42:11
When using the pipe operator %>% with packages such as dplyr , ggvis , dycharts , etc, how do I do a step conditionally? For example; step_1 %>% step_2 %>% if(condition) step_3 These approaches don't seem to work: step_1 %>% step_2 if(condition) %>% step_3 step_1 %>% step_2 %>% if(condition) step_3 There is a long way: if(condition) { step_1 %>% step_2 }else{ step_1 %>% step_2 %>% step_3 } Is there a better way without all the redundancy? Here is a quick example that takes advantage of the . and ifelse : X<-1 Y<-T X %>% add(1) %>% { ifelse(Y ,add(.,1), . ) } In the ifelse , if Y is TRUE if

Order of operation with piping

扶醉桌前 提交于 2019-11-26 17:52:35
问题 My question is where does the piping operator of magrittr package %>% come in the order of operations? I have a problem simmilar to the following: set.seed(10) df <- data.frame(a=rnorm(3),b=rnorm(3),c=rnorm(3)) df/rowSums(df) %>% round(.,3) This results in the following non rounded figures: a b c 1 -0.0121966 0.119878 0.8922125 To get the rounded figures I need to put df/rowSums(df) between brackets. I experimented with the + , - , * , / and ^ and from the results I found the order of

Error: could not find function “%>%”

无人久伴 提交于 2019-11-26 15:47:02
问题 I'm running an example in R, going through the steps and everything is working so far except for this code produces an error: words <- dtm %>% as.matrix %>% colnames %>% (function(x) x[nchar(x) < 20]) Error: could not find function "%>%" I don't understand what the benefit of using this special operator %>% is, and any feedback would be great. 回答1: You need to load a package (like magrittr or dplyr ) that defines the function first, then it should work. install.packages("magrittr") # package

R: use magrittr pipe operator in self written package

孤街浪徒 提交于 2019-11-26 15:10:55
I would like to use the pipe-operator %>% introduced in the magrittr package in a package I wrote myself to chain dplyr data transformations. magrittr is listed as Import in the DESCRIPTION file. After loading my own package and testing the function which uses the pipe-operator I get the following error message: Error in functionname(parameter, : could not find function "%>%" Changing %>% to magrittr::%>% in the function source code does not help either because the package cannot be built anymore. tonytonov It should have worked correctly if you had magrittr listed in Depends . However, this

R Conditional evaluation when using the pipe operator %>%

浪尽此生 提交于 2019-11-26 08:55:06
问题 When using the pipe operator %>% with packages such as dplyr , ggvis , dycharts , etc, how do I do a step conditionally? For example; step_1 %>% step_2 %>% if(condition) step_3 These approaches don\'t seem to work: step_1 %>% step_2 if(condition) %>% step_3 step_1 %>% step_2 %>% if(condition) step_3 There is a long way: if(condition) { step_1 %>% step_2 }else{ step_1 %>% step_2 %>% step_3 } Is there a better way without all the redundancy? 回答1: Here is a quick example that takes advantage of