Skip to content

Instantly share code, notes, and snippets.

View rhilfi's full-sized avatar

Roger Hilfiker rhilfi

View GitHub Profile
@rhilfi
rhilfi / rename_column_names_with_rename_all_and_str_replace
Created October 4, 2020 11:33
rename_column_names_with_rename_all_and_str_replace # R
library(tidyverse)
Dieser_Alte_Hase=rnorm(10,2,2)
Dieses_Alte_Pferd=rnorm(10,4,2)
Dieses_Alte_Haus=rnorm(10,4,2)
data<-data.frame(Dieser_Alte_Hase, Dieses_Alte_Pferd, Dieses_Alte_Haus)
names(data)
data<-data %>%
@rhilfi
rhilfi / stopInstallingPackagesOnOneDrive
Created November 5, 2020 04:30
stopInstallingPackagesOnOneDrive
https://medium.com/@ValidScience/how-to-fix-rstudios-package-installation-on-windows-10-c1e602bf3a1f
@rhilfi
rhilfi / save_plot_as_pdf_in_r
Last active November 7, 2020 11:30
save_plot_as_pdf #R
a<-rnorm(100, 30, 10)
hist(a, col="pink")
pdf("just_a_plain_histogram.pdf", width=8, height=5, useDingbats=FALSE)
hist(a, col="pink")
dev.off()
@rhilfi
rhilfi / remove_NA_in_String
Created November 13, 2020 15:15
remove_NA_in_String #R
# remove NA in string variables
library(dplyr)
Names = c("Peter","NA", "Gabriel", "John", "Lennon","Stuart")
Country=c("Sweden", "Switzerland", "Cumberland", "Wonderland", "Nowhereland", "Land")
Numbers=c(1,2,3,4,NA,6)
id<- 1:6
data<-data.frame(id, Names, Numbers, Country)
head(data)
@rhilfi
rhilfi / round_all_number_in_dataframe #R
Created November 30, 2020 03:44
round_all_number_in_dataframe #R
Name<-c("John", "Paul","George", "Ringo")
Age <-rnorm(4, 70, 2)
number_1<-c(2.34324,2.32145,5.432,5.3456)
number_2<-c(6.5432,8.32456,3.2346,9.3254245)
data<-data.frame(Name, Age, number_1, number_2)
data_rounded<-data %>%
mutate(across(where(is.numeric), round, 1))
@rhilfi
rhilfi / sum_of_some_columns_per_row #R
Created November 30, 2020 12:16
sum_of_some_columns_per_row #R
# Calculate sum of different columns per row
id<-1:5
food<-c(1,4,3,2,1)
drink<-c(4,3,2,4,5)
names<-c("John", "Mick", "Paul","George", "Ringo")
books<-c(3,4,5,4,2)
data<-data.frame(id, food, drink,names, books )
@rhilfi
rhilfi / calculate_with_mutate_at
Last active December 9, 2020 07:41
calculate_with_mutate_at #R
# calculate with mutate_at
# see also here: https://suzan.rbind.io/2018/02/dplyr-tutorial-2/
# create an example data frame ####
library(tidyverse)
id<-1:10
item1<-c(2,3,2,4,3,2,1,0,2,1)
item2<-c(4,3,2,1,2,3,1,0,1,2)
@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
@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 / 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)