R: transforming specific rows into columns

I have quite untidy data imported from messy json file, it looks like this: raw_df <- data.frame(text = c(paste0('text', 1:3), '---------- OUTCOME LINE ----------', paste0('text', 4:6), '---------- OUTCOME LINE ----------'), ...
more »

2017-11-13 11:11 (2) Answers

Extract longest word in string

I would like to find and extract the longest word of a string, if possible using a tidyverse package. library(tidyverse) tbl <- tibble(a=c("ab cde", "bcde f", "cde fg"), b=c("cde", "bcde", "cde")) tbl # A tibble: 3 x 1 a <chr> 1 ab cde...
more »

2017-11-06 09:11 (2) Answers

Conditional filtering using tidyverse

I want to filter my data frame based on a variable that may or may not exist. As an expected output, I want a df that is filtered (if it has the filter variable), or the original, unfiltered df (if the variable is missing). Here is a minimal exampl...
more »

2017-09-12 16:09 (2) Answers

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.tr <-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