Skip to content

Instantly share code, notes, and snippets.

Avatar

Roger Hilfiker rhilfi

View GitHub Profile
@rhilfi
rhilfi / summarise_n_if_missings
Created Jan 6, 2022
summarise_n_if_missings #R
View summarise_n_if_missings
# 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 Dec 2, 2021
as.numeric(factor)_zero_one_issue
View 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 Dec 2, 2021
working_with_dates_lubridate
View 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 Dec 2, 2021
illustration_scientific_notation
View 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 Oct 16, 2021
save_file_with_date_time #R
View save_file_with_date_time
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 Sep 11, 2021
simulation_case_when_rnorm #R
View simulation_case_when_rnorm
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 Jul 12, 2021
as numeric over several columns
View as.numericWithMutate_at
mutate(across(Age_mean:woman_perc, as.numeric))
@rhilfi
rhilfi / replace_empty_string_with_na_r
Created Apr 15, 2021
replace_empty_string_with_na_r #R
View replace_empty_string_with_na_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 Apr 8, 2021
lookfor_in_r_as_in_stata #r
View lookfor_in_r_as_in_stata
# 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 Feb 21, 2021
variable_in_Markdown #Markdown
View variable_in_Markdown
https://www.brianchildress.co/variables-in-markdown/#:~:text=The%20idea%20is%20pretty%20simple,later%20in%20the%20same%20document.
[variable]: 1