Skip to content

Instantly share code, notes, and snippets.

View rhilfi's full-sized avatar

Roger Hilfiker rhilfi

View GitHub Profile
@rhilfi
rhilfi / summarise_n_if_missings
Created January 6, 2022 09:17
summarise_n_if_missings #R
# http://www.cookbook-r.com/Manipulating_data/Summarizing_data/
library(tidyverse)
id<-1:100
group<-sample(c("Men", "Women"), 100, replace =TRUE)
age=rnorm(100, 50, 12)
data<-data.frame(id, group, age)
@rhilfi
rhilfi / as.numeric(factor)_zero_one_issue
Created December 2, 2021 14:54
as.numeric(factor)_zero_one_issue
numeric<-sample(c(0,1), 100, replace=TRUE)
factor<-factor(numeric, levels=c(0,1), labels=c("nein", "Ja"))
mean(numeric)
mean(factor)
mean(as.numeric(factor))
@rhilfi
rhilfi / working_with_dates_lubridate
Created December 2, 2021 14:35
working_with_dates_lubridate
library(tidyverse)
library(lubridate)
# you can find more information here:
# https://r4ds.had.co.nz/dates-and-times.html
# https://cran.r-project.org/web/packages/lubridate/vignettes/lubridate.html
# https://lubridate.tidyverse.org
data<-read.table(text="id Tag_Monat_Jahr Monat_Tag_Jahr dmy_hms dob dod date_event date_release_white_album start_rooftop_concert end_rooftop_concert
@rhilfi
rhilfi / illustration_scientific_notation
Created December 2, 2021 11:27
illustration_scientific_notation
# example to illustrate scientific notation
# https://stackoverflow.com/questions/5352099/how-to-disable-scientific-notation
scientificNotation=format(c(1000,100,10,1,0.1,0.01, 0.001, 0.0001, 0.00001, 0.000001, 0.0000001), scientific=TRUE)
nonScientificNotation=format(c(1000,100,10,1,0.1,0.01, 0.001, 0.0001, 0.00001, 0.000001, 0.0000001), scientific=FALSE)
data<-data.frame(nonScientificNotation, scientificNotation)
@rhilfi
rhilfi / save_file_with_date_time
Created October 16, 2021 09:19
save_file_with_date_time #R
library(rio)
library(tidyverse)
# the two lines with str_replace just show that the author of this code does not know how to correctly format date_time. Sorry for that.
st=format(Sys.time(), "%a %b %d %X %Y")
st=str_replace_all(st, ":","_")
st=str_replace_all(st, " ","_")
rio::export(check_error_start_intervention,paste("../02_data_checks/start_intervention_before_eval_initiale",st,".xlsx", sep="_"))
@rhilfi
rhilfi / simulation_case_when_rnorm
Created September 11, 2021 06:29
simulation_case_when_rnorm #R
library(rio)
library(tidyverse)
library(summarytools)
size=100000
age_years<-rnorm(size, mean=50, sd=12)
gender<-sample(c("1", "2"), size=size,prob=c(0.51, 0.49), replace=TRUE)
@rhilfi
rhilfi / as.numericWithMutate_at
Created July 12, 2021 12:54
as numeric over several columns
mutate(across(Age_mean:woman_perc, as.numeric))
@rhilfi
rhilfi / replace_empty_string_with_na_r
Created April 15, 2021 11:34
replace_empty_string_with_na_r #R
library(summarytools)
library(dplyr)
first_name=c("Hans", "Peter", "", "Fritz", "Susi")
family_name=c("Meier", "Muller", "Zgraggen", "", "Zurcher")
age=c(43,43,23,55,40)
data<-data.frame(first_name, family_name, age)
summarytools::freq(data$first_name)
@rhilfi
rhilfi / lookfor_in_r_as_in_stata
Created April 8, 2021 04:04
lookfor_in_r_as_in_stata #r
# install.packages("questionr")
library(questionr)
Thats_a_cool_variable =rnorm(100, 50,12)
Thats_a_less_cool_variable =rnorm(100,50,12)
Thats_an_even_cooler_variable=rnorm(100,40,21)
Does_R_makes_you_angry=rep(0:1,each=50)
Does_R_makes_you_happy=rep(c(1,0),each=50)
@rhilfi
rhilfi / variable_in_Markdown
Created February 21, 2021 09:04
variable_in_Markdown #Markdown
https://www.brianchildress.co/variables-in-markdown/#:~:text=The%20idea%20is%20pretty%20simple,later%20in%20the%20same%20document.
[variable]: 1