magrittr

Get name of dataframe passed through pipe in R

与世无争的帅哥 提交于 2019-11-30 12:56:40
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 would probably be enough to return the original call, which would include the mtcars %>% piece. This is

Why is using dplyr pipe (%>%) slower than an equivalent non-pipe expression, for high-cardinality group-by?

安稳与你 提交于 2019-11-30 12:53:06
问题 I thought that generally speaking using %>% wouldn't have a noticeable effect on speed. But in this case it runs 4x slower. library(dplyr) library(microbenchmark) set.seed(0) dummy_data <- dplyr::data_frame( id=floor(runif(10000, 1, 10000)) , label=floor(runif(10000, 1, 4)) ) microbenchmark(dummy_data %>% group_by(id) %>% summarise(list(unique(label)))) microbenchmark(dummy_data %>% group_by(id) %>% summarise(label %>% unique %>% list)) Without pipe: min lq mean median uq max neval 1.691441 1

Differences between %.% (dplyr) and %>% (magrittr)

柔情痞子 提交于 2019-11-30 12:04:57
问题 The dplyr package introduced the %.% operator to pass the left hand side as an argument of the function on the right hand side, similar to a *NIX pipe. The magrittr package is a much more lightweight package that exists to define only that pipe-like operator. Yet one uses %.% and the other %>% . Is there any difference between the two? Can I just use %>% even in dplyr code, or will that cause subtle bugs? On inspecting the code, they take very different paths early on, so simple eyeball

Custom pipe to silence warnings

半世苍凉 提交于 2019-11-30 11:38:58
Related to this question . I'd like to build a custom pipe %W>% that would silence warnings for one operation library(magrittr) data.frame(a= c(1,-1)) %W>% mutate(a=sqrt(a)) %>% cos will be equivalent to : w <- options()$warn data.frame(a= c(1,-1)) %T>% {options(warn=-1)} %>% mutate(a=sqrt(a)) %T>% {options(warn=w)} %>% cos These two tries don't work : `%W>%` <- function(lhs,rhs){ w <- options()$warn on.exit(options(warn=w)) options(warn=-1) lhs %>% rhs } `%W>%` <- function(lhs,rhs){ lhs <- quo(lhs) rhs <- quo(rhs) w <- options()$warn on.exit(options(warn=w)) options(warn=-1) (!!lhs) %>% (!

How do I access the data frame that has been passed to ggplot()?

六眼飞鱼酱① 提交于 2019-11-30 08:03:34
问题 I want to set the string N=xxx as the title of my figure, where xxx is the number of observations in the data frame that I pass as the data argument to ggplot() . In my current code, I explicitly pass that data frame a second time as an argument to sprintf() which I use inside of labs() : ggplot(mtcars, aes(mpg, hp)) + labs(title=sprintf("N=%i", nrow(mtcars))) + geom_point() This does produce the desired title, but it won't work with more complex tasks: I use a dplyr pipe to construct the

What is the difference between %>% and %,% in magrittr?

扶醉桌前 提交于 2019-11-30 04:58:45
Github developmental version of magrittr includes some cool new function for piping but I do not exactly catch de difference between %>% and %,% . Is this only formal with %>% for value and %,% for functions, or there is some specific peculiarity? The normal piping operator is %>% . You can use %,% to create a reusable pipe, a pipe without data. Then later you can use the same pipe with various data sets. Here is an example. library(magrittr) library(dplyr) library(Lahman) Suppose you want to calculate the top 5 baseball players, according to total hits. Then you can do something like this

How to extract / subset an element from a list with the magrittr %>% pipe?

徘徊边缘 提交于 2019-11-30 04:28:05
Since the introduction of the %>% operator in the magrittr package (and it's use in dplyr ), I have started to use this in my own work. One simple operation has me stumped, however. Specifically, this is the extraction (or subsetting) of elements from a list. An example: In base R I would use $ , [ or [[ to extract an element from a list: iris$Species iris[["Species"]] I can achieve the same using the %>% pipe: iris %>% subset(select = "Species") %>% head Species 1 setosa 2 setosa 3 setosa 4 setosa 5 setosa 6 setosa Or iris %>% `[[`("Species") %>% levels [1] "setosa" "versicolor" "virginica"

Differences between %.% (dplyr) and %>% (magrittr)

痞子三分冷 提交于 2019-11-30 01:59:40
The dplyr package introduced the %.% operator to pass the left hand side as an argument of the function on the right hand side, similar to a *NIX pipe. The magrittr package is a much more lightweight package that exists to define only that pipe-like operator. Yet one uses %.% and the other %>% . Is there any difference between the two? Can I just use %>% even in dplyr code, or will that cause subtle bugs? On inspecting the code, they take very different paths early on, so simple eyeball comparison would suggest that they're different. I haven't been able to find anything documented when I

Meaning of error using . shorthand inside dplyr function

青春壹個敷衍的年華 提交于 2019-11-29 11:14:27
I'm getting a dplyr::bind_rows error. It's a very trivial problem, because I can easily get around it, but I'd like to understand the meaning of the error message. I have the following data of some population groups for New England states, and I'd like to bind on a copy of these same values with the name changed to "New England," so that I can group by name and add them up, giving me values for the individual states, plus an overall value for the region. df <- structure(list(name = c("CT", "MA", "ME", "NH", "RI", "VT"), estimate = c(501074, 1057316, 47369, 76630, 141206, 27464)), class = c(

How do I access the data frame that has been passed to ggplot()?

强颜欢笑 提交于 2019-11-29 09:26:05
I want to set the string N=xxx as the title of my figure, where xxx is the number of observations in the data frame that I pass as the data argument to ggplot() . In my current code, I explicitly pass that data frame a second time as an argument to sprintf() which I use inside of labs() : ggplot(mtcars, aes(mpg, hp)) + labs(title=sprintf("N=%i", nrow(mtcars))) + geom_point() This does produce the desired title, but it won't work with more complex tasks: I use a dplyr pipe to construct the data frame that is being plotted, and as this is a time-consuming process, I wouldn't want to repeat the