magrittr

Use multiple command chains with piping

早过忘川 提交于 2020-01-04 08:23:09
问题 EDIT: I reworked the question to make it clearer and integrate what I found by myself Pipes are a great way to make the code more readable when using a single command chain In some cases however, I feel one is forced to be inconsistent to its philosophy, either by creating unnecessary temp variables, mixing piping and embedded parenthesis, or defining custom functions. See this SO question for example, where OP wants to know how to convert colnames to lower case with pipes: Dplyr or Magrittr

Convert column in data.frame to date

只愿长相守 提交于 2019-12-28 06:49:30
问题 My dataframe a1 <- c("a","a","b","b","c","d","e","e") b2 <- c("01.01.2015", "02.02.2015", "14.02.2012", "16.08.2008", "17.06.2003", "31.01.2015", "07.01.2022", "09.05.2001") c3 <- c("1a", "2b", "3c", "4d", "5e", "6f", "7g", "8h") d3 <- c(1:8) df2 <- data.frame(a1,b2,c3,d3, stringsAsFactors = F) My code. library(dplyr) library(magrittr) test <- df2 %>% group_by(a1) %>% as.Date(b2, format = "%d.%m.%Y") Error in as.Date.default(., b2, format = "%d.%m.%Y") : do not know how to convert '.' to

How to maintain defaults in a function when using the pipe in R?

一笑奈何 提交于 2019-12-24 12:03:42
问题 I feel like this should be a really easy task, but I can't seem to find the answer online. I simply want to do something like this: stringr::str_interp("x <- ${rnorm(1)}") %>% parse(text = .) %>% eval() But that doesn't work; when I call x it tells me it cannot be found. I know it's a valid series of functions because this works: eval(parse(text = stringr::str_interp("x <- ${rnorm(1)}"))) Any idea how to achieve this? Thanks! NOTE: I'm using the github version of stringr , which is where the

Error in magrittr pipe when using ``magrittr::`%>%` ``

♀尐吖头ヾ 提交于 2019-12-23 09:01:04
问题 For whatever reason I was playing with magrittr pipe syntax, and came across a weird error that occurs when you scope explicitly qualify the call to %>% . I know that using the syntax below destroys the purpose of the pipe, but I'm curious as to why the error occurs. The first call to sum works as expected and outputs a 1 . The second call causes the error: Error in pipes[[i]] : subscript out of bounds . library(magrittr) `%>%`(1,sum()) magrittr::`%>%`(1,sum()) Looking at the pipe's source

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

▼魔方 西西 提交于 2019-12-19 03:25:21
问题 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)

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

☆樱花仙子☆ 提交于 2019-12-18 11:42:19
问题 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

Meaning of error using . shorthand inside dplyr function

不问归期 提交于 2019-12-18 06:53:16
问题 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",

Pipe in magrittr package is not working for function load()

断了今生、忘了曾经 提交于 2019-12-18 05:53:36
问题 It seem %>% in the magrittr package is not working for the function load() . This is my minimal example to reproduce my question. ## Create two example variables and save to tempdir() a <- 1 b <- 1 save(list = ls(), file = file.path(tempdir(), 'tmp.RData')) ## Remove all variables and load into global environment # rm(list = ls()) load(file.path(tempdir(), 'tmp.RData')) ls() # [1] "a" "b" # Write the same code with pipe "%>%", but not variable is loaded # rm(list =ls()) library(magrittr)

Chain arithmetic operators in dplyr with %>% pipe

可紊 提交于 2019-12-18 04:29:20
问题 I would like to understand why, in the the dplyr or magrittr package, and more specifically the chaining function %>% has some trouble with the basic operators + , - , * , and / Chaining takes the output of previous statement and feeds it as first argument of the next: 1:10 %>% sum # [55] Thus how come this doesn't work 1:10 %>% *2 %>% sum 1:10 %>% .*2 %>% sum I also found that the following syntax works for adding/substracting, but not multiply or divide. why so? 1:10 %>% +(2) # works OK 1

Adding prefix or suffix to most data.frame variable names in piped R workflow

我们两清 提交于 2019-12-17 10:55:09
问题 I want to add a suffix or prefix to most variable names in a data.frame, typically after they've all been transformed in some way and before performing a join. I don't have a way to do this without breaking up my piping. For example, with this data: library(dplyr) set.seed(1) dat14 <- data.frame(ID = 1:10, speed = runif(10), power = rpois(10, 1), force = rexp(10), class = rep(c("a", "b"),5)) I want to get to this result (note variable names): class speed_mean_2014 power_mean_2014 force_mean