tidyverse

Non-standard eval in dplyr::mutate

你离开我真会死。 提交于 2019-12-05 20:49:53
In theory this should work, as I've read the tidyverse guide on NSE, but it throws me an error as seen in the bottom of this example. Why is this? I understand how to do a simple quasiquotation of an object, but I do not understand how to evaluate a fraction of two quasiquoted objects. Can anyone help with this? tmp <- structure(list(qa11a = structure(c(1616, 7293, 1528, 1219, 2049, 286), label = "Total voters removed from Nov. 2008 to Nov. 2010", class = c("labelled","numeric")), state_abbv = c("AL", "AL", "AL", "AL", "AL", "AL"), fipscode = c("0100100000", "0100300000", "0100500000",

Match character vector in a dataframe with another character vector and trim character

一笑奈何 提交于 2019-12-05 18:47:35
Here is a dataframe and a vector. df1 <- tibble(var1 = c("abcd", "efgh", "ijkl", "qrst")) vec <- c("abcd", "mnop", "ijkl") Now, for all the values in var1 that matches with the values in vec, keep only first 3 characters in var1 such that the desired solution is: df2 <- tibble(var1 = c("abc", "efgh", "ijk", "qrst")) Since, "abcd" matches, we keep only 3 characters i.e. "abc" in df2, but "efgh" doesn't exist in vec, so we keep it as is i.e "efgh" in df2. How can I use dplyr and/or stringr to accomplish this? You can just use %in% to check whether the strings are in the vector, and substr to

write_csv read_csv with scientific notation after 1000th row

只愿长相守 提交于 2019-12-05 14:09:24
Writing a data frame with a mix of small integer entries (value less than 1000) and "large" ones (value 1000 or more) into csv file with write_csv() mixes scientific and non-scientific entries. If the first 1000 rows are small values but there is a large value thereafter, read_csv() seems to get confused with this mix and outputs NA for scientific notations: test_write_read <- function(small_value, n_fills, position, large_value) { tib <- tibble(a = rep(small_value, n_fills)) tib$a[position] <- large_value write_csv(tib, "tib.csv") tib <- read_csv("tib.csv") } The following lines do not make

Faster method than “while” loop to find chain of infection in R

这一生的挚爱 提交于 2019-12-05 13:01:56
I'm analyzing large tables (300 000 - 500 000 rows) that store data output by a disease simulation model. In the model, animals on a landscape infect other animals. For example, in the example pictured below, animal a1 infects every animal on the landscape, and the infection moves from animal to animal, branching off into "chains" of infection. In my example below, I want to take the table that stores information about each animal (in my example below, table = allanimals ) and slice out just the information about animal d2 's chain of infection (I've highlighted d2 's chain in green) so I can

Convert a tidy table to deeply nested list using R and tidyverse

时间秒杀一切 提交于 2019-12-05 12:38:42
I am trying to convert a tidy table (eg. example below) into a nested list using R/tidyverse. Using some tidyverse magic I was able to convert it to a list nested of depth three, but I cannot figure out how to nest it deeper. Take the following example input: library(tidyverse) library(stringi) n_patient = 2 n_samples = 3 n_readgroup = 4 n_mate = 2 df = data.frame(patient = rep(rep(LETTERS[1:n_patient], n_samples),2), sample = rep(rep(seq(1:n_samples), each = n_patient),2), readgroup = rep(stri_rand_strings(n_patient * n_samples * n_readgroup, 6, '[A-Z]'),2), mate = rep(1:n_mate, each = n

Gathering specific pairs of columns into rows by dplyr in R [duplicate]

孤人 提交于 2019-12-05 10:46:25
This question already has an answer here: Reshaping multiple sets of measurement columns (wide format) into single columns (long format) 7 answers I am trying to convert a data frame from wide to long format by gathering specific pairs of columns of which example is shown below: An example of data frame df <- data.frame(id=c(1,2,3,4,5), var=c("a","d","g","f","i"),a1=c(3,5,1,2,2), b1=c(2,4,1,2,3), a2=c(8,1,2,5,1), b2=c(1,6,4,7,2), a3=c(7,7,2,3,1), b3=c(1,1,4,9,6)) Initial table: id var a1 b1 a2 b2 a3 b3 1 1 a 3 2 8 1 7 1 2 2 d 5 4 1 6 7 1 3 3 g 1 1 2 4 2 4 4 4 f 2 2 5 7 3 9 5 5 i 2 3 1 2 1 6

Computation failed in `stat_smooth()`: object 'C_crspl' not found

时光毁灭记忆、已成空白 提交于 2019-12-05 10:25:23
I am trying to add a geom_smooth() to a qplot() with the following code: library(ggplot2) library(ggplot2movies) qplot(votes, rating, data = movies) + geom_smooth() However, the smoother is missing from the plot. I also receive the following warning message: Computation failed in stat_smooth() : object 'C_crspl' not found Does anybody know what is wrong here? This is my setup: > sessionInfo() R version 3.4.1 (2017-06-30) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 16.04.1 LTS I had similar issues: # `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")' #

Joining list of data.frames from map() call

折月煮酒 提交于 2019-12-05 09:31:15
Is there a "tidyverse" way to join a list of data.frames (a la full_join() , but for >2 data.frames)? I have a list of data.frames as a result of a call to map() . I've used Reduce() to do something like this before, but would like to merge them as part of a pipeline - just haven't found an elegant way to do that. Toy example: library(tidyverse) ## Function to make a data.frame with an ID column and a random variable column with mean = df_mean make.df <- function(df_mean){ data.frame(id = 1:50, x = rnorm(n = 50, mean = df_mean)) } ## What I'd love: my.dfs <- map(c(5, 10, 15), make.df) #%>% # <

How do pipes work with purrr map() function and the “.” (dot) symbol

℡╲_俬逩灬. 提交于 2019-12-05 08:53:59
When using both pipes and the map() function from purrr, I am confused about how data and variables are passed along. For instance, this code works as I expect: library(tidyverse) cars %>% select_if(is.numeric) %>% map(~hist(.)) Yet, when I try something similar using ggplot, it behaves in a strange way. cars %>% select_if(is.numeric) %>% map(~ggplot(cars, aes(.)) + geom_histogram()) I'm guessing this is because the "." in this case is passing a vector to aes(), which is expecting a column name. Either way, I wish I could pass each numeric column to a ggplot function using pipes and map().

How to import ical .ics file in R

给你一囗甜甜゛ 提交于 2019-12-05 07:58:31
I would like to import a .ics file into R, however, when I try to do so like... sneak_cal <- read.delim("iCal-TribeEvents.ics", sep = ":", header=FALSE, stringsAsFactors = FALSE, strip.white = TRUE, na.strings = "") ...I end up splitting the character strings of website (belonging to the X-ORIGINAL-URL or the UID field) too, which is undesirable ie https and //www.kicksonfire.com The ultimate goal is to get the data into a tidy format where each row represents a single VEVENT , which I think would be represented by a unique UID , without any loss of information (such as the URL) Is there