Posts

Showing posts with the label dplyr

How can I stop by code blocks from splitting in preview of R Notebook?

Image
Clash Royale CLAN TAG #URR8PPP How can I stop by code blocks from splitting in preview of R Notebook? ```{r scatterplots, collapse=TRUE, results= 'hold'} p1 <- ggplot(x, aes(y=f.ecdf, x=P2))+geom_point()+theme_bw() p2 <- ggplot(x, aes(y=f.ecdf, x=P2))+geom_point()+theme_bw() p3 <- ggplot(x, aes(y=f.ecdf, x=G1))+geom_point()+theme_bw() p4 <- ggplot(x, aes(y=f.ecdf, x=G2))+geom_point()+theme_bw() p5 <- ggplot(x, aes(y=f.ecdf, x=P2))+geom_point()+theme_bw() p6 <- ggplot(x, aes(y=f.ecdf, x=P2))+geom_point()+theme_bw() p7 <- ggplot(x, aes(y=f.ecdf, x=G1))+geom_point()+theme_bw() p8 <- ggplot(x, aes(y=f.ecdf, x=G2))+geom_point()+theme_bw() grid.arrange( p1, p2, p3, p4, ncol = 2 ) grid.arrange( p5, p6, p7, p8, ncol = 2 ) ``` But in preview the code chunks are being evaluated after splitting. How can I stop this from happening. Basically, I want all plots to flow interrupted. What other information can I provide to diagnose this? ...

How to change the value in a row if condition met in the previous row under dplyr

Image
Clash Royale CLAN TAG #URR8PPP How to change the value in a row if condition met in the previous row under dplyr Is there any alternative way to replace the following codes under dplyr to avoid explicit loop and data name to achieve the following? This is to create an adjusted date, if the condition of the current supp_date less than the previous supp_date + tablet is met. test$adj_fill_dt <- as.Date(NA, "%Y-%m-%d") test$adj_fill_dt[1] <- test$supp_date[1] for(i in 2:6) { if (test[i, "supp_date"] < test[i-1, "adj_fill_dt"] + test[i-1, "tablet"]) { test[i, "adj_fill_dt"] <- test[i-1, "adj_fill_dt"] + test[i-1, "tablet"] } else { test[i, "adj_fill_dt"] <- test[i, "supp_date"] } } From: supp_date tablet 2017-07-19 30 2017-08-07 30 2017-09-08 30 2017-10-11 30 2017-11-08 30 2017-12-07 30 To: supp_date tablet adj_fill_dt 2017-07-19 30 201...

Manipulating dates alongside consecutive results

Image
Clash Royale CLAN TAG #URR8PPP Manipulating dates alongside consecutive results I need some help working with consecutive results. Here is my sample data: df <- structure(list(idno = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2), result = structure(c(1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L), .Label = c("Negative", "Positive" ), class = c("ordered", "factor")), samp_date = structure(c(15909, 15938, 15979, 16007, 16041, 16080, 16182, 16504, 16576, 16645, 16721, 16745, 17105, 17281, 17416, 17429), class = "Date")), class = "data.frame", row.names = c(NA, -16L)) The 'idno' represents individual people who had a test with 'result' on a given date ('samp_date'). From each individual person, I need to find the earliest consecutive 'Negatives' and return the date of the first 'negative' result. To return this date, the consecutive negatives must span >30 days wi...

Update/Replace Values in Dataframe with Tidyverse Join

Image
Clash Royale CLAN TAG #URR8PPP Update/Replace Values in Dataframe with Tidyverse Join What is the most efficient way to update/replace NAs in main dataset with (correct) values in a lookup table? This is such a common operation! Similar questions do not seem to have tidy solutions. Constraints: 1) Please assume a large number of missing values and bigger lookup table than the example given. So case-wise replacement operations would be impractical (no case_when , if_else , etc.) case_when if_else 2)The lookup table does not have all values of main dataframe, only the replacement ones. Tidyverse solution answer much preferred. Similar questions do not seem to have tidy solutions. library(tidyverse) ### Main Dataframe ### df1 <- tibble( state_abbrev = state.abb[1:10], state_name = c(state.name[1:5], rep(NA, 3), state.name[9:10]), value = sample(500:1200, 10, replace=TRUE) ) #> # A tibble: 10 x 3 #> state_abbrev state_name value #> <chr> <chr> ...

Mutate value only for numeric columns AND only in the first and last row of a data frame using dplyr

Image
Clash Royale CLAN TAG #URR8PPP Mutate value only for numeric columns AND only in the first and last row of a data frame using dplyr Given a data frame like: library(dplyr) library(lubridate) df <- data.frame( date = seq(ymd('2018-01-01'), ymd('2018-01-10'), by = 'days'), location = "AMS", V1 = seq(1:10), V2 = seq(11:20) ) I would like to use dplyr to change the value of the first and last row, and only in numeric columns. dplyr I can do it for one column, as in: df %>% mutate(V1 = ifelse(row_number()==1, mean(V1)*100, V1)) %>% mutate(V1 = ifelse(row_number()==nrow(.), mean(V1)*100, V1)) However I cannot manage to find a way to use mutate_at or mutate_if to do that for all numeric columns at once. Could you help me with that? mutate_at mutate_if I suspect you don't mean to use seq() for V1 and V2 , as the : notation already returns a vector – zack 16 mins ago...