Using dplyr functions on variables named "."

Sometimes when generating a data frame from a list, the variable is named "." by default. How can I refer to this variable within dplyr functions, if only to change the variable name to something more appropriate. # Code that produces my data frame...
more »

2017-08-31 03:08 (2) Answers

take mean of variable defined by string in dplyr

Seems like this should be easy but I'm stumped. I've gotten the rough hang of programming with dplyr 0.7, but struggling with this: How do I program in dplyr if the variable I want to program with will be a string? I am scraping a database, and fo...
more »

2017-07-23 01:07 (2) Answers

Using dplyr filter() in programming

I am writing my function and want to use dplyr's filter() function to select rows of my data frame that satisfy a condition. This is my code: library(tidyverse) df <-data.frame(x = sample(1:100, 50), y = rnorm(50), z = sample(1:100,50), w = samp...
more »

2017-07-21 03:07 (2) Answers

use dplyr mutate() in programming

I am trying to assign a column name to a variable using mutate. df <-data.frame(x = sample(1:100, 50), y = rnorm(50)) new <- function(name){ df%>%mutate(name = ifelse(x <50, "small", "big")) } When I run new(name = "newVar") ...
more »

2017-07-18 16:07 (3) Answers

R dplyr summarise bug?

library(tidyverse) stats <- read_csv('stats.csv') ## Warning: Installed Rcpp (0.12.12) different from Rcpp used to build dplyr (0.12.11). ## Please reinstall dplyr to avoid random crashes or undefined behavior. I am pretty sure that I got the s...
more »

2017-07-18 05:07 (2) Answers

How to reorder factor levels in a tidy way?

Hi I usually use some code like the following to reorder bars in ggplot or other types of plots. Normal plot (unordered) library(tidyverse) <-iris %>% group_by(Species) %>% mutate(mSW = mean(Sepal.Width)) %>% select(mSW,Spec...
more »

2017-07-17 18:07 (2) Answers

Naming a new variable based on a quosure

I'm trying to write a custom function that will compute a new variable based on values from a predefined vector of variables (e.g., vector_heavy) and then name the new variable based on an argument provided to the function (e.g., custom_name). This...
more »

2017-07-03 22:07 (1) Answers

tidyr::gather na.rm with missing data

Let's say I have multiple columns in a data frame that measure the same concept, but in different methods (e.g. there are multiple kinds of IQ tests, and students could have any one of them, or none at all). I want to combine the various methods int...
more »

2017-05-25 23:05 (3) Answers

Reduce lists by summing element-wise in purrr

I'm trying to use purrr to sum list elements with the same index. This can be achieved in base R using the following: xx <- list(a = c(1,2,3,4,5), b = c(1,2,3,4,5)) Reduce("+", xx) which provides: [1] 2 4 6 8 10 Great! That's what I need...
more »

2017-04-11 18:04 (1) Answers

R Conditional Counter based on multiple columns

I have a data frame with multiple responses from subjects (subid), which are in the column labelled trials. The trials count up and then start over within one subject. Here's an example dataframe: subid <- rep(1:2, c(10,10)) trial <- rep(1:5,...
more »

2017-03-25 19:03 (1) Answers

Extract value from data frame?

If I have a data frame, the standard approach to extracting a value is to use a Boolean/logical expression to match the correct row and column. Example: set.seed(1) df <- data.frame(letters = letters[1:3], numbers = as.character(c(1, 2, 1)), valu...
more »

2017-03-01 08:03 (0) Answers

Transform named list of tables into data.frame

I have a named list of tables like this: # make this simple and reproducible set.seed(1) days <- c("mon", "tue", "wed", "thu", "fri", "sat", "sun") # create list of tables mylist <- list( one = table(sample(days, 3, replace = TRUE)), two ...
more »

2017-02-27 15:02 (3) Answers

Map for nested `data_frame`s

When using map on a nested data_frame, I do not understand why the latter two version give an error: library(tidyverse) # dummy data df <- tibble(id = rep(1:10, each = 10), val = runif(100)) df <- nest(df, -id) # works as ex...
more »

2017-02-22 08:02 (1) Answers