tidyverse

working with lists of models using the pipe syntax

≯℡__Kan透↙ 提交于 2019-12-10 14:47:37
问题 I often like to fit and examine multiple models that relate two variables in an R dataframe. I can do that using syntax like this: require(tidyverse) require(broom) models <- list(hp ~ exp(cyl), hp ~ cyl) map_df(models, ~tidy(lm(data=mtcars, formula=.x))) But I'm used to the pipe syntax and was hoping to be able to something like this: mtcars %>% map_df(models, ~tidy(lm(data=., formula=.x))) That makes it clear that I'm "starting" with mtcars and then doing stuff to it to generate my output.

Vectorised time zone conversion with lubridate

守給你的承諾、 提交于 2019-12-10 14:19:49
问题 I have a data frame with a column of date-time strings: library(tidyverse) library(lubridate) testdf = data_frame( mytz = c('Australia/Sydney', 'Australia/Adelaide', 'Australia/Perth'), mydt = c('2018-01-17T09:15:00', '2018-01-17T09:16:00', '2018-01-17T09:18:00')) testdf # A tibble: 3 x 2 # mytz mydt # <chr> <chr> # 1 Australia/Sydney 2018-01-17T09:15:00 # 2 Australia/Adelaide 2018-01-17T09:16:00 # 3 Australia/Perth 2018-01-17T09:18:00 I want to convert these date-time strings to POSIX date

Restructuring Data for Ggplot2 Combination Grouped and Stacked Barchart Using Tidyverse [closed]

Deadly 提交于 2019-12-10 12:17:54
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . library(tidyverse) library(ggplot2) I am attempting to create the bar chart below but I'm having trouble restructuring the data. I provided some sample data below which I created kind of fast, so the results may be strange, but I'm more interested in how to use tidyverse tools to

Error in mutate_impl(.data, dots) : Evaluation error: Only year, quarter, month, week, and day periods are allowed for an index of class Date

大憨熊 提交于 2019-12-10 10:26:47
问题 I am using Anomalize package to detect the Anomalies, but I am getting the mentioned error even though I have defined the Date as index : Sample Code : x <- as.data.frame(data %>% group_by(date,acc_id) %>% summarise(count = as.numeric(n_distinct(d_id))) %>% ungroup()) x$acc_id <- as.character(x$acc_id) x <- x %>% tibbletime::as_tbl_time(index = date) x %>% time_decompose(count, method = "twitter", trend = "2 months") %>% anomalize(remainder, method = "gesd") %>% time_recompose() %>% plot

Correlation matrix with dplyr, tidyverse and broom - P-value matrix

一个人想着一个人 提交于 2019-12-10 10:09:12
问题 all. I want to obtain the p-value from a correlation matrix using dplyr and/or broom packages and testing multiple variables at the same time . I'm aware of other methods, but dplyr seems easier and more intuitive for me. In addition, dplyr will need to correlate each variable to obtain the specific p-value, what makes the process easier and faster. I checked other links, but they did not work for this question (example 1, example 2, example 3) When I use this code, the correlation

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

纵饮孤独 提交于 2019-12-10 08:31:15
问题 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

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

自闭症网瘾萝莉.ら 提交于 2019-12-10 07:14:25
问题 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

How to import ical .ics file in R

纵饮孤独 提交于 2019-12-10 04:28:51
问题 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

Using R with tidyquant and massiv data

与世无争的帅哥 提交于 2019-12-09 21:41:40
问题 While working with R I encountered a strange problem: I am processing date in the follwing manner: Reading data from a database into a dataframe, filling missing values, grouping and nesting the data to a combined primary key, creating a timeseries and forecastting it for every group, ungroup and clean the data, write it back into the DB. Somehting like this: https://cran.rstudio.com/web/packages/sweep/vignettes/SW01_Forecasting_Time_Series_Groups.html For small data sets this works like a

Does a multi-value purrr::pluck exist?

丶灬走出姿态 提交于 2019-12-09 17:33:13
问题 Seems like a basic question and perhaps I'm just missing something obvious ... but is there any way to pluck a sublist (with purrr )? More specifically, here's an initial list: l <- list(a = "foo", b = "bar", c = "baz") And I want to return a new (sub-)list with only elements a and b . Normally, I'd just do 'base' R sub-listing: l[c("a", "b")] But this doesn't provide the nice .default handling of pluck . My understanding is that pluck 'replaces' [[ , but is there a purrr equivalent for