magrittr

dplyr with subgroup join

大兔子大兔子 提交于 2019-12-10 21:45:14
问题 The following problem can be seen as a "two-column reshape to wide", and there are several methods available to solve it the classical way, from base::reshape (horror) to reshape2 . For the two-group case, a simple subgroup join works best. Can I reformulate the join within the piping framework of dplyr ? The example below is a bit silly, but I needed the join in a longer pipe-chain which I do not want to break. library(dplyr) d = data.frame(subject= rep(1:5,each=2),treatment=letters[1:2],bp

Use $ dollar sign at end of of an R magrittr pipeline to return a vector

笑着哭i 提交于 2019-12-10 17:47:35
问题 I'd like to use $ at the end of a magrittr / tidyverse pipeline. $ works directly next to tidyverse functions like read_csv and filter , but as soon I create a pipeline with %>% it raises an error. Here is a simple reproducible example. # Load libraries and create a dummy data file library(dplyr) library(readr) write_csv(data_frame(x=c(0,1), y=c(0,2)), 'tmp.csv') # This works y <- read_csv('tmp.csv')$y str(y) # This also works df_y <- read_csv('tmp.csv') y <- filter(df_y, y > 0)$y str(y) #

Using `%>%` with `lm` and `rbind`

蹲街弑〆低调 提交于 2019-12-10 17:26:26
问题 I have a dataframe Z looking like t x y d 0 1 2 1 1 2 3 1 2 3 4 1 0 1 2 2 1 2 3 2 2 3 4 2 with d being a factor column. I know want to fit a linear model with lm to y over t for both factors in d and add it as a new column to the dataframe. I tried Z %>% filter(d == 1) %>% lm(y ~ t) but this gives me an error saying "Error in as.data.frame.default(data) : cannot coerce class ""formula"" to a data.frame" . But lm(y ~ t, data = Z) works fine. Any help would be appreciated. 回答1: We need to

What does the magrittr dot/period (“.”) operator do when it's at the very beginning of a pipeline?

和自甴很熟 提交于 2019-12-10 16:44:57
问题 I don't understand what the . in the following code is doing or where to find documentation for it: library(tidyverse) ggplot(iris) + geom_point( aes(x=Sepal.Length, y=Sepal.Width), data = . %>% filter(Species == 'setosa') ) This appears to be behaving quite differently from the usage described in What does the dplyr period character "." reference? where the . does not appear in the left-hand-most position. The docs here say merely A pipeline with a dot (.) as LHS will create a unary function

R pipe (%>%) capabilities - storage and partial use?

天大地大妈咪最大 提交于 2019-12-10 16:24:06
问题 I like the idea of the pipe improving readability, but I've had difficulty using it because of how inflexible it feels. So far I've only succeeded when my goal is to straightforwardly pipe X through a set of functions h(g(f(x,foo),bar),stuff) x %>% f(foo) %>% g(bar) %>% h(stuff) If I want to store an intermediate output so that I can have h(x,stuff,f(x,foo)), is that possible? I've tried x %>% intermediate = f(foo) %>% g(bar) but that fails. Assign doesn't work because the first argument is

Referencing piped dataset in ggplot layers for subsetting

我怕爱的太早我们不能终老 提交于 2019-12-10 11:14:30
问题 Trying to find a way to reference different parts of the dataset for different ggplot2 geom layers without having to save the dataset first in the global environment. Ex non working solution. read_excel("Book1.xlsx",sheet = "Sheet2") %>% ggplot(aes(x,y)) + geom_col() + geom_point(data=subset($ID == "1"),colour="red") Above does not seem to work since i am not referencing the piped(magrittr) dataset in a way that R can recognize it. I have searched but the only solutions i could see are based

dplyr + magrittr + qplot = no plot?

柔情痞子 提交于 2019-12-09 18:33:56
问题 I want to use qplot (ggplot2) and then forward the data with magrittr : This works: mtcars %>% qplot(mpg, cyl, data=.) This produces an error: mtcars %>% qplot(mpg, cyl, data=.) %>% summarise(mean(mpg)) And those produce only summary statistics: mtcars %T>% qplot(mpg, cyl, data=.) %>% summarise(mean(mpg)) mtcars %>% {qplot(mpg, cyl, data=.); .} %>% summarise(mean(mpg)) mtcars %T>% {qplot(mpg, cyl, data=.)} %>% summarise(mean(mpg)) What is the problem? I already found this solution, but it

Parallel wilcox.test using group_by and summarise

扶醉桌前 提交于 2019-12-09 12:41:43
问题 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

Dplyr or Magrittr - tolower?

天涯浪子 提交于 2019-12-09 05:09:09
问题 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

How to add a row names to a data frame in a magrittr chain

房东的猫 提交于 2019-12-09 00:49:19
问题 I want to do the opposite of: Convert row names into first column Somewhere down the chain of pipes I would like to add row names to the data frame, for example, I would like to do the following actions using pipes: rownames(mtcars) <- as.character(1:nrow(mtcars)) so that it looks like: library(magrittr) mtcars <- mtcars %>% ...??? Note that @akrun's answer shows that if the pipe is used in conjection with a function that coerces the data frame into a tbl_df , the row names will be lost. 回答1: