magrittr

Is right-to-left operator associativity in R possible?

↘锁芯ラ 提交于 2019-12-03 16:16:33
问题 I'm new to R, and I just discovered I suffer from Bracket Phobia (see comment in the link). I like the way magrittr notation %>% works, because it avoids nested parenthesis in some situations, and makes code more readable. I came from Mathematica , where there is a very similar native // notation to do what %>% does. Here are some R and Mathematica comparisons: #R Notation c(1.5,-2.3,3.4) %>% round %>% abs %>% sum #Mathematica Notation {1.5,-2.3,3.4}//Round//Abs//Total So far so good, but, my

Parallel wilcox.test using group_by and summarise

杀马特。学长 韩版系。学妹 提交于 2019-12-03 15:06:39
There must be an R-ly way to call wilcox.test over multiple observations in parallel using group_by. I've spent a good deal of time reading up on this but still can't figure out a call to wilcox.test that does the job. Example data and code below, using magrittr pipes and summarize() . library(dplyr) library(magrittr) # create a data frame where x is the dependent variable, id1 is a category variable (here with five levels), and id2 is a binary category variable used for the two-sample wilcoxon test df <- data.frame(x=abs(rnorm(50)),id1=rep(1:5,10), id2=rep(1:2,25)) # make sure piping and

Dplyr or Magrittr - tolower?

蓝咒 提交于 2019-12-03 05:02:28
Is it possible to set all column names to upper or lower within a dplyr or magrittr chain? In the example below I load the data and then, using a magrittr pipe, chain it through to my dplyr mutations. In the 4th line I use the tolower function , but this is for a different purpose: to create a new variable with lowercase observations. mydata <- read.csv('myfile.csv') %>% mutate(Year = mdy_hms(DATE), Reference = (REFNUM), Event = tolower(EVENT) I'm obviously looking for something like colnames = tolower but know this doesn't work/exist. I note the dplyr rename function but this isn't really

how to feed the result of a pipe chain (magrittr) to an object

只愿长相守 提交于 2019-12-03 04:56:14
问题 This is a fairly simply question. But I couldn't find the answer per google/stackexchange and looking at the documentation of magrittr. How do you feed the result of a chain of functions which are connected via %>% to create a vector? what I saw most people do is: a <- data.frame( x = c(1:3), y = (4:6)) %>% sum() but is there also a solution where I can just pipe-chain the result to feed it to an object, maybe an alias or sth of the like, somewhat like this: data.frame( x = c(1:3), y = (4:6))

Is right-to-left operator associativity in R possible?

筅森魡賤 提交于 2019-12-03 04:41:59
I'm new to R, and I just discovered I suffer from Bracket Phobia (see comment in the link). I like the way magrittr notation %>% works, because it avoids nested parenthesis in some situations, and makes code more readable. I came from Mathematica , where there is a very similar native // notation to do what %>% does. Here are some R and Mathematica comparisons: #R Notation c(1.5,-2.3,3.4) %>% round %>% abs %>% sum #Mathematica Notation {1.5,-2.3,3.4}//Round//Abs//Total So far so good, but, my question is: Is there some way to mimic Mathematica @ notation , with right-to-left associativity in R

How to use `package::function()` inside a package when `function` is an infix operator?

孤人 提交于 2019-12-02 06:09:51
问题 According to H. Wickham's book R Packages , in the Package Metadata chapter, on how to add a package dependency, Hadley points out good reasons to explicitly refer to external functions using the syntax package::function() . Adding a package dependency here ensures that it’ll be installed. However, it does not mean that it will be attached along with your package (i.e., library(x)). The best practice is to explicitly refer to external functions using the syntax package::function() . This

Using table() in dplyr chain

北城以北 提交于 2019-12-01 18:19:39
问题 Can someone explain why table() doesn't work inside a chain of dplyr-magrittr piped operations? Here's a simple reprex: tibble( type = c("Fast", "Slow", "Fast", "Fast", "Slow"), colour = c("Blue", "Blue", "Red", "Red", "Red") ) %>% table(.$type, .$colour) Error in sort.list(y) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list? But this works of course: df <- tibble( type = c("Fast", "Slow", "Fast", "Fast", "Slow"), colour = c("Blue", "Blue", "Red", "Red", "Red") ) table(df

Using table() in dplyr chain

ぃ、小莉子 提交于 2019-12-01 18:16:40
Can someone explain why table() doesn't work inside a chain of dplyr-magrittr piped operations? Here's a simple reprex: tibble( type = c("Fast", "Slow", "Fast", "Fast", "Slow"), colour = c("Blue", "Blue", "Red", "Red", "Red") ) %>% table(.$type, .$colour) Error in sort.list(y) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list? But this works of course: df <- tibble( type = c("Fast", "Slow", "Fast", "Fast", "Slow"), colour = c("Blue", "Blue", "Red", "Red", "Red") ) table(df$type, df$colour) Blue Red Fast 1 2 Slow 1 1 This behavior is by design: https://github.com/tidyverse

Pipe a data frame to a function whose argument pipes a dot

折月煮酒 提交于 2019-12-01 18:04:05
问题 How can one pipe a data frame to a function whose argument pipes a dot? mpg %>% rbind(., . %>% rev()) Error in rep(xi, length.out = nvar) : attempt to replicate an object of type 'closure' Another example: mpg %>% { . %>% arrange(manufacturer) } Functional sequence with the following components: arrange(., manufacturer) Use 'functions' to extract the individual functions. 回答1: Wrap the dot to be piped in parentheses like (.) : mpg %>% rbind(., (.) %>% rev()) Or, for lambda function: mpg %>% {

Pipe a data frame to a function whose argument pipes a dot

人盡茶涼 提交于 2019-12-01 18:00:15
How can one pipe a data frame to a function whose argument pipes a dot? mpg %>% rbind(., . %>% rev()) Error in rep(xi, length.out = nvar) : attempt to replicate an object of type 'closure' Another example: mpg %>% { . %>% arrange(manufacturer) } Functional sequence with the following components: arrange(., manufacturer) Use 'functions' to extract the individual functions. Wrap the dot to be piped in parentheses like (.) : mpg %>% rbind(., (.) %>% rev()) Or, for lambda function: mpg %>% { (.) %>% arrange(manufacturer) } 来源: https://stackoverflow.com/questions/37757191/pipe-a-data-frame-to-a