Skip to content

Instantly share code, notes, and snippets.

View rhilfi's full-sized avatar

Roger Hilfiker rhilfi

View GitHub Profile
@rhilfi
rhilfi / mutate_across_str_replace
Last active January 25, 2022 11:10
mutate_across_str_replace #R
library(dplyr)
# see https://www.tidyverse.org/blog/2020/04/dplyr-1-0-0-colwise/
# see https://vbaliga.github.io/replace-text-in-specific-column/
ID<-1:3
Names<-c("Peter", "Paul", "Marhy")
FamilyNames<-c("Lennon", "McCartney", "Jagger")
Age<-rnorm(3, 80, 12)
data<-data.frame(Names, FamilyNames, ID, Age)
@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 / pivot_longer_several_columns_names_pattern_numbers #R
Last active August 19, 2021 14:45
pivot_longer_several_columns_names_pattern_numbers #R
library(tidyverse)
# thanks for the help:
# https://community.rstudio.com/t/pivot-longer-on-multiple-column-sets-pairs/43958/9
# https://cran.r-project.org/web/packages/stringr/vignettes/regular-expressions.html
walking_speed_1<-c(0.3,0.5,0.7)
balance_four_balance_test_1<-c(1,2,1)
tug_1<-c(10,20,12)
walking_speed_2<-c(0.4,0.6,0.8)
@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)