magrittr

What does the %<>% operator mean in R? [closed]

牧云@^-^@ 提交于 2019-12-01 08:29:01
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . What does the %<>% operator do in R ? What's the difference between using %<>% and <- ? In what type of circumstances %<>% could be useful ? 回答1: The help, ?magrittr::`%<>%` , answers all your questions, if you are refering to magrittr`s compound assignment pipe-operator: [...] %<

Print data frame dimensions at each step of filtering

旧街凉风 提交于 2019-12-01 06:22:04
问题 I am using the tidyverse to filter out a dataframe and would like a print at each step of the dimensions (or nrows) of the intermediate objects. I thought I could simply use a tee pipe operator from magrittr but it doesn't work. I think I understand the concept behind the tee pipe but can't figure out what is wrong. I searched extensively but didn't find much resources about the tee pipe. I built a simple example with the mtcars dataset. Printing the intermediate objects works but not if I

Easily finding and replacing every match in a nested list

你。 提交于 2019-12-01 04:23:39
Take this object as an example: expr <- substitute(mean(exp(sqrt(.)), .)) It is a nested list. I want to find every element that matches quote(.) . For example, magrittr 's solution matches only the first level of the call: dots <- c(FALSE, vapply(expr[-1], identical, quote(.), FUN.VALUE = logical(1))) dots [1] FALSE FALSE TRUE But I wanted to find every "." in an arbitrary nested list. In this particular case this would be these two dots: expr[[3]] expr[[2]][[2]][[2]] And then these dots should be replaced: expr[[3]] <- as.name("replacement") expr[[2]][[2]][[2]] <- as.name("replacement") expr

Stepping through a pipeline with intermediate results

拥有回忆 提交于 2019-12-01 03:24:54
Is there a way to output the result of a pipeline at each step without doing it manually? (eg. without selecting and running only the selected chunks) I often find myself running a pipeline line-by-line to remember what it was doing or when I am developing some analysis. For example: library(dplyr) mtcars %>% group_by(cyl) %>% sample_frac(0.1) %>% summarise(res = mean(mpg)) # Source: local data frame [3 x 2] # # cyl res # 1 4 33.9 # 2 6 18.1 # 3 8 18.7 I'd to select and run: mtcars %>% group_by(cyl) and then... mtcars %>% group_by(cyl) %>% sample_frac(0.1) and so on... But selecting and CMD

Combining pipes and the magrittr dot (.) placeholder

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 02:04:49
问题 I am fairly new to R and I am trying to understand the %>% operator and the usage of the " . " (dot) placeholder. As a simple example the following code works library(magrittr) library(ensurer) ensure_data.frame <- ensures_that(is.data.frame(.)) data.frame(x = 5) %>% ensure_data.frame However the following code fails ensure_data.frame <- ensures_that(. %>% is.data.frame) data.frame(x = 5) %>% ensure_data.frame where I am now piping the placeholder into the is.data.frame method. I am guessing

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

风流意气都作罢 提交于 2019-11-30 23:54:24
问题 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) 回答1: The problem isn't map , but rather how the %>%

Stepping through a pipeline with intermediate results

北战南征 提交于 2019-11-30 22:48:40
问题 Is there a way to output the result of a pipeline at each step without doing it manually? (eg. without selecting and running only the selected chunks) I often find myself running a pipeline line-by-line to remember what it was doing or when I am developing some analysis. For example: library(dplyr) mtcars %>% group_by(cyl) %>% sample_frac(0.1) %>% summarise(res = mean(mpg)) # Source: local data frame [3 x 2] # # cyl res # 1 4 33.9 # 2 6 18.1 # 3 8 18.7 I'd to select and run: mtcars %>% group

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

不打扰是莪最后的温柔 提交于 2019-11-30 22:27:17
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. You can use row.names<- : mtcars <- mtcars %>% `row.names<-`(as.character(1:nrow(mtcars))) Should work. As

R: Further subset a selection using the pipe %>% and placeholder

左心房为你撑大大i 提交于 2019-11-30 21:20:19
I recently discovered the pipe operator %>% , which can make code more readable. Here is my MWE . library(dplyr) # for the pipe operator library(lsr) # for the cohensD function set.seed(4) # make it reproducible dat <- data.frame( # create data frame subj = c(1:6), pre = sample(1:6, replace = TRUE), post = sample(1:6, replace = TRUE) ) dat %>% select(pre, post) %>% sapply(., mean) # works as expected However, I struggle using the pipe operator in this particular case dat %>% select(pre, post) %>% cohensD(.$pre, .$post) # piping returns an error cohensD(dat$pre, dat$post) # classical way works

How to set the row names of a data frame passed on with the pipe %>% operator?

为君一笑 提交于 2019-11-30 13:15:55
问题 I have a data frame which I am dcast ing using the reshape2 package, and I would like to remove the first column and have it become the row names of the data frame instead. Original dataframe, before dcast : > corner(df) ID_full gene cpm 1 S36-A1 DDX11L1 0 2 S36-A1 WASH7P 0 3 S36-A1 MIR1302-2 0 4 S36-A1 FAM138A 0 5 S36-A1 OR4F5 0 pivot function to dcast the table: library(reshape2) pivot <- function(x){ castTable <- x %>% dcast(ID_full ~ gene, value.var="cpm") } After dcast , wrapped in my