[Solved] Find new and active user each week from user_id and date

so I managed to calculate the count_new by checking the first appearance of a user_id and then merging with initial data adding a column that tell if a user is new by date and id then I counted the new by date: library(dplyr) firstshow<-Orders %>% group_by(user_id) %>% arrange(date) %>% slice(1L) %>% mutate(new = “new”) newdata<-merge.data.frame(Orders,firstshow,by=c(“date”,”user_id”),all … Read more

[Solved] Calculate conditional mean in R with dplyr (like group by in SQL) [duplicate]

I think what you are looking for (if you want to use dplyr) is a combination of the functions group_byand mutate. library(dplyr) city <- c(“a”, “a”, “b”, “b”, “c”) temp <- 1:5 df <- data.frame(city, temp) df %>% group_by(city) %>% mutate(mean(temp)) Which would output: city temp mean(temp) (fctr) (int) (dbl) 1 a 1 1.5 2 … Read more

[Solved] Working for single input row but not for multiple

Here you go. I acknowledge you specified a loop in your question, but in R I avoid loops wherever possible. This is better. This uses plyr::join_all to join all your data frames by Item and LC, then dplyr::mutate to do the calculations. Note you can put multiple mutations in one mutate() function: library(plyr) library(dplyr) library(tidyr) … Read more

[Solved] Complete missing values in time series using previous day data – using R

If I understand correctly, the trick is that you want to fill downward except for the bottommost NAs. And the problem with tidyr‘s fill is that it goes all the way down. This isn’t a fully-tidyverse solution, but for this data: library(dplyr) library(tidyr) data <- tribble( ~Date, ~time_series_1, ~time_series_2, as.Date(“2019-01-01”), NA, 10, as.Date(“2019-02-01”), 5, NA, … Read more

[Solved] is there a R code for group_by and combinations

Introduction Group_by and combinations are two powerful tools in the R programming language. They allow users to quickly and easily manipulate data and create meaningful insights. Group_by allows users to group data by one or more variables, while combinations allow users to create all possible combinations of a set of variables. In this article, we … Read more

[Solved] Ifelse to Compare the content of columns with dplyr

The best way to do this would be to use the glue package. This allows you to easily assign variable names based on strings: library(tidyverse) library(glue) > df <- data.frame( sample_id = c(‘SB013’, ‘SB014’, ‘SB013’, ‘SB014’, ‘SB015’, ‘SB016’, ‘SB016’), IN = c(1,2,3,4,5,6,7), OUT = c(rep(‘out’,7))) > df sample_id IN OUT 1 SB013 1 out 2 … Read more

[Solved] Substitute LHS of = in R

1) eval/parse Create a cmd string, parse it and evaluate it: f2 <- function(DF, x, env = parent.frame()){ cmd <- sprintf(“mutate(%s, %s = mean(v1))”, deparse(substitute(DF)), x) eval(parse(text = cmd), env) } f2(DF, “v1_name”) giving v1 v1_mean 1 1 2 2 2 2 3 3 2 … etc … 2) eval/as.call Another way is to construct … Read more