tidyverse

Running multiple glm models on mixed data with purrr

亡梦爱人 提交于 2021-01-29 12:12:02
问题 Suppose we have a toy data set: library(tidyverse) library(purrr) tbl <- tibble(a = rep(c(0, 1), each = 5), b = rep(c(0, 1), times = 5), c = runif(10), d = rexp(10)) %>% mutate_at(vars(1,2), as.factor) where a is a dependent variable and b:d are independent variables. The idea is to run glm model for each independent variable: glm(a ~ b, data = tbl, family = "binomial") glm(a ~ c, data = tbl, family = "binomial") glm(a ~ d, data = tbl, family = "binomial") My initial attempt goes as follows:

Editable calculation with DT table in Shiny

天涯浪子 提交于 2021-01-29 08:47:50
问题 I've been at this for awhile and have read a bunch but I still can't wrap my head around how to make this work. Is there a simple solution? I want to edit a DT table in my shiny app and, upon editing, I'd like there to be a change in a column that aggregates two values. Here is an example: library(tidyverse) library(shiny) library(DT) mt <- mtcars %>% select(mpg, cyl) %>% head() ui <- fluidPage( DTOutput(outputId = "final_tbl") ) server <- function(input, output){ dat <- reactive({ d <- mt %>

R, How to accumulate values in a list column, based on multiple criteria

时光毁灭记忆、已成空白 提交于 2021-01-29 06:37:15
问题 I have a dataset of patients getting treatments in various hospitals (in-patient only) wherein some analysis has revealed several inconsistencies. One of these was that - software was allowing patients to get admission without closure of their previously open case_id . In order to understand it better, let us consider the sample dataset sample data dput(df) df <- structure(list(case_id = 1:22, patient_id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 3L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 7L, 8L,

R dates as column names containing duplicate values (need to retain original date)

时光毁灭记忆、已成空白 提交于 2021-01-29 05:31:56
问题 I have a dataset I'm trying to tidy up. I read in the file with read.xlsx, contained in the header is date values that I need to retain their values even when duplicated when I gather/spread the data. The data set looks like the below. The dates from excel read in as numbers (which is fine) the issue is that there can be duplicate dates (e.g. 43693) , which I need to keep their original values. Date 43693 43686 43686 43714 43693 1 Contract 111 222 333 444 555 2 Org1 NR NB NR NB P 3 Org2 P P P

R dplyr window function, get the first value in the next x window that fulfil some condition

烂漫一生 提交于 2021-01-28 22:01:02
问题 I have some dplyr dataframe and I have some condition. I want to know for each cell what is the index of the first cell that matches the condition in the next x rows. In my case, I want to have an additional column that holds the index of the first value that was larger than the current value in at least z. Example: here we are looking for the index of the first value in the next 3 rows that is larger by at least 3 from the current value. In the case of the first row, the value is 0 and the

R dplyr window function, get the first value in the next x window that fulfil some condition

随声附和 提交于 2021-01-28 21:42:14
问题 I have some dplyr dataframe and I have some condition. I want to know for each cell what is the index of the first cell that matches the condition in the next x rows. In my case, I want to have an additional column that holds the index of the first value that was larger than the current value in at least z. Example: here we are looking for the index of the first value in the next 3 rows that is larger by at least 3 from the current value. In the case of the first row, the value is 0 and the

Trouble installing tidyverse in R

牧云@^-^@ 提交于 2021-01-28 18:01:52
问题 I'm having trouble installing tidyverse in R. When installing the package, the installation starts but stops after a while and nothing happens. It seems like it's still in "installation mode" since I cannot to anything else (when I try to create a variable, it doesn't show up in the global environment/nothing happens). What am I doing wrong? 回答1: It must be an internet connection error. Trying installing it in this way: install.packages("devtools") devtools::install_github("hadley/tidyverse")

Achieving the equivalent of rbind using tidyr [duplicate]

放肆的年华 提交于 2021-01-28 10:44:43
问题 This question already has answers here : tidyverse pivot_longer several sets of columns, but avoid intermediate mutate_wider steps [duplicate] (3 answers) Closed 5 months ago . I have some data that looks like this. set.seed(1) df <- data.frame(group = rep(letters[1:2],each=3), day = rep(1:3,2), var1_mean = round(rnorm(6),2), var1_sd = round(rnorm(6,5),2), var2_mean = round(rnorm(6),2), var2_sd = round(rnorm(6,5),2)) df # output # group day var1_mean var1_sd var2_mean var2_sd # a 1 -0.63 5.49

Achieving the equivalent of rbind using tidyr [duplicate]

我只是一个虾纸丫 提交于 2021-01-28 10:42:43
问题 This question already has answers here : tidyverse pivot_longer several sets of columns, but avoid intermediate mutate_wider steps [duplicate] (3 answers) Closed 5 months ago . I have some data that looks like this. set.seed(1) df <- data.frame(group = rep(letters[1:2],each=3), day = rep(1:3,2), var1_mean = round(rnorm(6),2), var1_sd = round(rnorm(6,5),2), var2_mean = round(rnorm(6),2), var2_sd = round(rnorm(6,5),2)) df # output # group day var1_mean var1_sd var2_mean var2_sd # a 1 -0.63 5.49

Calculate differences based on categorical column with tidyverse

狂风中的少年 提交于 2021-01-28 08:25:16
问题 I have the following data frame: library(tidyverse) df <- data.frame( vars = rep(letters[1:2], 3), value = c(10,12,15,19,22,23), phase = rep(factor(c("pre","post1","post2"), levels = c("pre","post1","post2")),2) ) %>% arrange(vars,phase) And I would like to calculate the difference in value of the following: post1 - pre post2 - post1 post2 - pre for each var (i.e., a and b ). What would be the most efficient way of achieving this using tidyverse ? Expected outcome: vars x diffs a post1 - pre