Skip to content

Instantly share code, notes, and snippets.

View rhilfi's full-sized avatar

Roger Hilfiker rhilfi

View GitHub Profile
@rhilfi
rhilfi / 0_reuse_code.js
Created May 24, 2017 09:04
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@rhilfi
rhilfi / colorGradientGenerator
Created August 25, 2019 06:29
generate gradients of color from one color #colours #colors
https://mycolor.space
@rhilfi
rhilfi / keepRowsWithNonMissingValues
Last active December 31, 2019 06:42
keep rows with at least one non-missing value #
# adapted from: https://stackoverflow.com/questions/44077122/r-select-rows-with-non-na-values-in-at-least-one-of-the-four-columns
a<-c(NA,NA,NA,NA,2,5,4,3,NA,NA)
b<-c(NA,NA,NA,NA,2,3,4,5,NA,NA)
c<-c(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA)
d<-c(1,4,3,1,2,4,3,2,1,NA)
df<-cbind(a,b,c,d)
df_atLeastOneNonMissingValue <- df[rowSums(is.na(df)) != ncol(df),] ## just to keep rows with at least one non-missing value
nrow(df)
nrow(df_atLeastOneNonMissingValue)
@rhilfi
rhilfi / putLastColumnFirst
Created December 31, 2019 15:27
putLastColumnFirst #R #dplyr
## adapted from: https://stackoverflow.com/questions/22286419/move-a-column-to-first-position-in-a-data-frame
## see also: https://stackoverflow.com/questions/37171891/how-does-dplyrs-select-helper-function-everything-differ-from-copying
# create data.frame to illustrate
a<-1:5
b<-a
c<-as.character(b)
df<-as.data.frame(cbind(a,b,c))
# end create data.frame to illustrate
df
@rhilfi
rhilfi / sampleSizeCalculationAUC
Created April 29, 2020 10:18
sampleSizeCalculationAUC
## AUC expected versus AUC minimal
## sample size calculatino AUC
## we use formula 7.5 in http://dx.doi.org/10.1016/j.jbi.2014.02.013
## Variance of AUC, following Appendix A (nonparametric AUC)
minimalAcceptableAUC<-0.7
expectedAUC<-0.8
ratioControlToCases<-2
@rhilfi
rhilfi / generateDataFrame_random_error_bias_a_bit_of_both
Created May 5, 2020 13:20
generateDataFrame_random_error_bias_a_bit_of_both
#####################################################################################################
### This exercise should illustrate perfect agreement, random error and systematic error ###
#####################################################################################################
library(rio)
library(tidyverse)
library(ggplot2)
library(BlandAltmanLeh)
library(psych)
rnorm2 <- function(n,mean,sd) { mean+sd*scale(rnorm(n)) } # this function is just to make it possible to fix the values or rnorm https://stackoverflow.com/questions/18919091/generate-random-numbers-with-fixed-mean-and-sd?lq=1
@rhilfi
rhilfi / label_variables_with_data_in_second_row
Created May 8, 2020 02:47
label_variables_with_data_in_second_row #R
#########################################
### Example comments and labels ###
#########################################
## Hier lassen wir R nachschauen, ob die benötigten Packages installiert sind, falls nein: installieren wir sie.
list.of.packages <- c("Hmisc","janitor", "tidyverse", "rio")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
# update.packages(ask=TRUE)
## If you have a csv with variable labels on the second row and you want label the variables
@rhilfi
rhilfi / remove_all_non_numbers_from_selected_columns_in_R
Created July 7, 2020 05:15
remove_all_non_numbers_from_selected_columns #r
#### Remove all non-number ####
library(tidyverse)
library(readr)
numbers<-c(1,2,3,4,5,6,7,8,9,10,11)
numbers_with_some_characters<-c("1","2zwei","3","4vier","5","6 sechs","7","8 ", " 9", "1 0", " 1 1 ")
data_to_show_remove_nonnumbers<-data.frame(numbers, numbers_with_some_characters)
@rhilfi
rhilfi / tabulate_absolute_and_relative_frequencies_R
Created August 17, 2020 15:08
tabulate_absolute_and_relative_frequencies_R #R
list.of.packages <- c("summarytools")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])] # https://stackoverflow.com/questions/4090169/elegant-way-to-check-for-missing-packages-and-install-them
if(length(new.packages)) install.packages(new.packages)
summarytools::freq(models$significant) # https://cran.r-project.org/web/packages/summarytools/vignettes/Introduction.html
gender<-c(rep("men",10), rep("women", 20), rep(NA,5))
@rhilfi
rhilfi / move_delete_files_from_within_R
Created September 9, 2020 13:03
move_delete_files_from_within_R #R
# see http://theautomatic.net/2018/07/11/manipulate-files-r/#:~:text=Files%20can%20be%20deleted%20with,add%20the%20parameter%20recursive%20%3D%20TRUE.