magrittr

Chain arithmetic operators in dplyr with %>% pipe

廉价感情. 提交于 2019-11-29 05:30:37
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:10 %>% *(2) # nope... So should I write an anonymous function even to do a *2 operation on my data.frame

%>% key binding / keyboard shortcut in Rstudio

允我心安 提交于 2019-11-29 02:52:11
I've been experimenting quite a bit with the increasingly popular %>% operator from the magrittr package. I've used it enough that I've set a keyboard shortcut to save me typing: shift + command + . instead of space , shift + 5 , shift + . , shift + 5 , space . This is great in SublimeTetxt2 but Rstudio does not allow services it does not work if I'm working within Rstudio projects. So my question is: Can you define text-inserting key-bindings or shortcuts within Rstudio ? This would be exactly synonymous with the alt + - binding for the assignment <- operator that is oxygen to the otter . If

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

夙愿已清 提交于 2019-11-29 02:33:09
问题 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? 回答1: 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

use %>% with replacement functions like colnames()<-

若如初见. 提交于 2019-11-28 15:37:56
问题 How can I use the pipe operator to pipe into replacement function like colnames()<- ? Here's what I'm trying to do: library(dplyr) averages_df <- group_by(mtcars, cyl) %>% summarise(mean(disp), mean(hp)) colnames(averages_df) <- c("cyl", "disp_mean", "hp_mean") averages_df # Source: local data frame [3 x 3] # # cyl disp_mean hp_mean # 1 4 105.1364 82.63636 # 2 6 183.3143 122.28571 # 3 8 353.1000 209.21429 But ideally it would be something like: averages_df <- group_by(mtcars, cyl) %>%

rollmean with dplyr and magrittr

扶醉桌前 提交于 2019-11-28 09:18:59
Given the following data: set.seed(1) data <- data.frame(o=c('a','a','a','a','b','b','b','b','c','c','c','c'), t=c(1,2,3,4,1,2,3,4,1,2,3,4), u=runif(12), v=runif(12)) data o t u v 1 a 1 0.26550866 0.6870228 2 a 2 0.37212390 0.3841037 3 a 3 0.57285336 0.7698414 4 a 4 0.90820779 0.4976992 5 b 1 0.20168193 0.7176185 6 b 2 0.89838968 0.9919061 7 b 3 0.94467527 0.3800352 8 b 4 0.66079779 0.7774452 9 c 1 0.62911404 0.9347052 10 c 2 0.06178627 0.2121425 11 c 3 0.20597457 0.6516738 12 c 4 0.17655675 0.1255551 I want to calculate the rolling mean (package zoo) of u per group defined by the coloumn o.

Error: could not find function “%>%”

六眼飞鱼酱① 提交于 2019-11-28 04:50:23
I'm running an example in R, going through the steps and everything is working so far except for this code produces an error: words <- dtm %>% as.matrix %>% colnames %>% (function(x) x[nchar(x) < 20]) Error: could not find function "%>%" I don't understand what the benefit of using this special operator %>% is, and any feedback would be great. You need to load a package (like magrittr or dplyr ) that defines the function first, then it should work. install.packages("magrittr") # package installations are only needed the first time you use it install.packages("dplyr") # alternative installation

Order of operation with piping

丶灬走出姿态 提交于 2019-11-28 01:56:56
My question is where does the piping operator of magrittr package %>% come in the order of operations? I have a problem simmilar to the following: set.seed(10) df <- data.frame(a=rnorm(3),b=rnorm(3),c=rnorm(3)) df/rowSums(df) %>% round(.,3) This results in the following non rounded figures: a b c 1 -0.0121966 0.119878 0.8922125 To get the rounded figures I need to put df/rowSums(df) between brackets. I experimented with the + , - , * , / and ^ and from the results I found the order of operation is as follow: Exponents Piping Multiplication and division Addition and subtraction Is that right or

Convert column in data.frame to date

家住魔仙堡 提交于 2019-11-28 01:33:33
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 class “Date” Well, I tried without the pipe: df$b2 <- as.Date(df$b2, format = "%d.%m.%Y") Error in df$b2 :

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

人走茶凉 提交于 2019-11-27 21:29:38
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_2014 1 a 0.5572500 0.8 0.5519802 2 b 0.2850798 0.6 1.0888116 My current approach is: means14 <- dat14 %>

%>% key binding / keyboard shortcut in Rstudio

本秂侑毒 提交于 2019-11-27 17:10:24
问题 I've been experimenting quite a bit with the increasingly popular %>% operator from the magrittr package. I've used it enough that I've set a keyboard shortcut to save me typing: shift + command + . instead of space , shift + 5 , shift + . , shift + 5 , space . This is great in SublimeTetxt2 but Rstudio does not allow services it does not work if I'm working within Rstudio projects. So my question is: Can you define text-inserting key-bindings or shortcuts within Rstudio ? This would be