how to subset a varible in R inside summarize

Advertisements for some reason this very basic subset is not working. Can anyone replicate the error that I find? I simply trying to summarize a variable that meets a condition. library(tidyverse) dat |> summarise( invoice_usd=sum(invoice_usd,na.rm=TRUE) ,invoice_usd_overdue = sum(invoice_usd[overdue_indicator==0],na.rm = TRUE) ) I get the following results Invoice_usd <dbl> invoice_usd_overdue <dbl> 3525924 3525924 Both are equal… Read More how to subset a varible in R inside summarize

Fill NAs when you have zeros in your dataset

Advertisements Suppose you have the following dataframe: df <- data.frame(year=c(rep(2010,12),rep(2011,12),rep(2012,12)), country=c(rep("DEU",4),rep("ITA",4),rep("USA",4), rep("DEU",4),rep("ITA",4),rep("USA",4), rep("DEU",4),rep("ITA",4),rep("USA",4)), industry=c(rep(1:4,9)), stock1=c(rep(0,24),0,0,2,4,1,0,1,2,3,3,3,5), stock2=c(rep(0,24),0,3,3,4,5,0,1,1,2,2,2,5)) and you want to get the following outcome: df2 <- data.frame(year=c(rep(2010,12),rep(2011,12),rep(2012,12)), country=c(rep("DEU",4),rep("ITA",4),rep("USA",4), rep("DEU",4),rep("ITA",4),rep("USA",4), rep("DEU",4),rep("ITA",4),rep("USA",4)), industry=c(rep(1:4,9)), stock1=c(rep(NA,24),0,0,2,4,1,0,1,2,3,3,3,5), stock2=c(rep(NA,24),0,3,3,4,5,0,1,1,2,2,2,5)) The concept is that if, for a particular year, a specific country reports zeros in stock2 across ALL industries, then those zeros… Read More Fill NAs when you have zeros in your dataset

ggplot how to plot ribbons in R from dataframe

Advertisements I have dataframe which has Category avg_fc,avg_la,sd_fc,sd_la values in column Category. df = data.frame (Date=c("2020-01-10","2020-01-10","2020-01-10","2020-01-10" ,"2020-01-11","2020-01-11","2020-01-11","2020-01-11", "2020-01-12","2020-01-12","2020-01-12","2020-01-12" ,"2020-01-13","2020-01-13","2020-01-13","2020-01-13"), Category=c("avg_fc","avg_la","sd_fc","sd_la","avg_fc","avg_la","sd_fc","sd_la","avg_fc", "avg_la","sd_fc","sd_la","avg_fc","avg_la","sd_fc","sd_la"), Value=c(25.5,40.5, 8.1,4.3, 29.5 ,31.5,5.6,9.1, 20.5,43.5, 4.1,8.3, 35.5 ,38.5,2.6,3.1)) From the given dataframe I would like to generate a ggplot where Category avg_fc and avg_la are the geom_line and Category sd_fc and sd_la become the… Read More ggplot how to plot ribbons in R from dataframe

Selection of days in which one hour meets the condition

Advertisements I have data with this structure (real data is more complicated): df1 <- read.table(text = "DT odczyt.1 odczyt.2 ‘2023-08-14 00:00:00’ 362 1.5 ‘2023-08-14 23:00:00’ 633 4.3 ‘2023-08-15 05:00:00’ 224 1.6 ‘2023-08-15 23:00:00’ 445 5.6 ‘2023-08-16 00:00:00’ 182 1.5 ‘2023-08-16 23:00:00’ 493 4.3 ‘2023-08-17 05:00:00’ 434 1.6 ‘2023-08-17 23:00:00’ 485 5.6 ‘2023-08-18 00:00:00’ 686 1.5… Read More Selection of days in which one hour meets the condition

Read multiple values as character vectors from an external file

Advertisements I am currently reading data from a .csv that looks like this: param_file <- tribble( ~variable, ~value, "year", "2023", "version", "Saint_XL, Sinner_XY", "metric", "ATE, OFCE" ) I then need to load the variable and its value as character vectors so I am doing this, with dplyr: year <- param_file %>% filter(variable == "year") %>%… Read More Read multiple values as character vectors from an external file