magrittr

filter for complete cases in data.frame using dplyr (case-wise deletion)

六眼飞鱼酱① 提交于 2019-12-17 04:46:48
问题 Is it possible to filter a data.frame for complete cases using dplyr? complete.cases with a list of all variables works, of course. But that is a) verbose when there are a lot of variables and b) impossible when the variable names are not known (e.g. in a function that processes any data.frame). library(dplyr) df = data.frame( x1 = c(1,2,3,NA), x2 = c(1,2,NA,5) ) df %.% filter(complete.cases(x1,x2)) 回答1: Try this: df %>% na.omit or this: df %>% filter(complete.cases(.)) or this: library(tidyr

What does %>% function mean in R?

柔情痞子 提交于 2019-12-17 03:46:09
问题 I have seen the use of %>% (percent greater than percent) function in some packages like dplyr and rvest . What does it mean? Is it a way to write closure blocks in R? 回答1: %...% operators %>% has no builtin meaning but the user (or a package) is free to define operators of the form %whatever% in any way they like. For example, this function will return a string consisting of its left argument followed by a comma and space and then it's right argument. "%,%" <- function(x, y) paste0(x, ", ",

R: use magrittr pipe operator in self written package

本秂侑毒 提交于 2019-12-17 03:00:56
问题 I would like to use the pipe-operator %>% introduced in the magrittr package in a package I wrote myself to chain dplyr data transformations. magrittr is listed as Import in the DESCRIPTION file. After loading my own package and testing the function which uses the pipe-operator I get the following error message: Error in functionname(parameter, : could not find function "%>%" Changing %>% to magrittr::%>% in the function source code does not help either because the package cannot be built

R: use magrittr pipe operator in self written package

随声附和 提交于 2019-12-17 03:00:15
问题 I would like to use the pipe-operator %>% introduced in the magrittr package in a package I wrote myself to chain dplyr data transformations. magrittr is listed as Import in the DESCRIPTION file. After loading my own package and testing the function which uses the pipe-operator I get the following error message: Error in functionname(parameter, : could not find function "%>%" Changing %>% to magrittr::%>% in the function source code does not help either because the package cannot be built

Copy column data when function unaggregates a single row into multiple in R

空扰寡人 提交于 2019-12-13 07:56:50
问题 I need help in taking an annual total (for each of many initiatives) and breaking that down to each month using a simple division formula. I need to do this for each distinct combination of a few columns while copying down the columns that are broken from annual to each monthly total. The loop will apply the formula to two columns and loop through each distinct group in a vector. I tried to explain in an example below as it's somewhat complex. What I have : | Init | Name | Date |Total Savings

Subsetting vectors with extract

筅森魡賤 提交于 2019-12-13 01:13:26
问题 Imagine I have vector, and I want to remove a specific element. I could do the following library(magrittr) foo <- LETTERS[1:10] foo %>% { bar <- . bar %>% extract(bar %>% equals("A") %>% not) } [1] "B" "C" "D" "E" "F" "G" "H" "I" "J" But if I'd like to be more succinct, this: foo %>% extract(. %>% equals("A") %>% not) doesn't work: Error in extract(., . %>% equals("A") %>% not) : invalid subscript type 'closure' Isn't there are more idiomatically magrittr 'y way to do this? 回答1: One option

Multiple ggplots with magrittr tee operator

懵懂的女人 提交于 2019-12-12 08:37:31
问题 I am trying to figure out why the tee operator, %T>%, does not work when I pass the data to a ggplot command. This works fine library(ggplot2) library(dplyr) library(magrittr) mtcars %T>% qplot(x = cyl, y = mpg, data = ., geom = "point") %>% qplot(x = mpg, y = cyl, data = ., geom = "point") And this also works fine mtcars %>% {ggplot() + geom_point(aes(cyl, mpg)) ; . } %>% ggplot() + geom_point(aes(mpg, cyl)) But when I use the tee operator, as below, it throws "Error: ggplot2 doesn't know

R wanting to limit the amount of digits from csv file

点点圈 提交于 2019-12-11 19:03:39
问题 There are plenty of threads about people thinking they have lost digits when reading in a csv file, and it's just a digits setting that isn't displaying all of them. I on the other hand want to round or truncate my incoming data to two decimal places. I am having an issue in Rmarkdown where I can't limit the decimal places after highlighting fields. This has lead to me attempting to round before the highlighting, but that leads to undesirable results if I go lower than 4 places since I am

R: use magrittr pipe operator in self written package

可紊 提交于 2019-12-11 17:32:27
问题 I would like to use the pipe-operator %>% introduced in the magrittr package in a package I wrote myself to chain dplyr data transformations. magrittr is listed as Import in the DESCRIPTION file. After loading my own package and testing the function which uses the pipe-operator I get the following error message: Error in functionname(parameter, : could not find function "%>%" Changing %>% to magrittr::%>% in the function source code does not help either because the package cannot be built

Weird error with lapply and dplyr/magrittr

☆樱花仙子☆ 提交于 2019-12-11 12:13:19
问题 Here's a piece of code: data <- data.frame(a=runif(20),b=runif(20),subject=rep(1:2,10)) %>% group_by(subject) %>% do(distance = dist(.)) #no dplyr intermediate <- lapply(data$distance,as.matrix) mean.dists <- apply(simplify2array(intermediate),MARGIN = c(1,2),FUN=mean) #dplyr mean.dists <- lapply(data$distance,as.matrix) %>% apply(simplify2array(.),MARGIN=c(1,2),FUN=mean) Why does the "no dplyr" version work, and the "dplyr" version throws the error, "dim(X) must have a positive length"? They