Skip to content

Instantly share code, notes, and snippets.

@sokarob
sokarob / Create Summary with Aggregate Function.r
Created May 3, 2024 21:28
[Create Summary with Aggregate Function] #aggregate #summary #group
df2 <- aggregate(df$points, by=list(df$team, df$position), FUN=mean)
#credit to https://www.statology.org/aggregate-r/
@sokarob
sokarob / Convert Month Number to Month Name.r
Created May 1, 2024 15:39
[Convert Month Number to Month Name] #month #name #convert
# Use month.name to change the index of months to its corresponding name
month.name[v1]
# Use month.abb for 3 character abbreviation for month
month.abb[v1]
# credit to: https://stackoverflow.com/questions/50607659/convert-months-number-to-month-name
# Example using YYYYMM format to make Month + Year:
@sokarob
sokarob / Find a String in Another String (Contains).R
Created April 24, 2024 21:12
[Find a String in Another String (Contains)] #find #contains
# Simple Condition
str_detect(`Column`,'STRING')==T
# Condition with case handling
str_detect(toupper(`Column`),'STRING')==T
# Also works for excluding records
str_detect(`Column`,'STRING')==F
@sokarob
sokarob / Turn Dataframe Columns Into Vectors.r
Last active March 22, 2024 15:26
[Turn Dataframe Columns Into Vectors] #dataframe #column #vector #coordinates
#use $ operator
new_vector <- df$column_name
#Example for geographic x and y coordinates:
x <- location$longitude
y <- location$latitude
#use indexing
new_vector <- df[['column_name']]
@sokarob
sokarob / Import SAS Data (.sas7bdat).r
Created March 22, 2024 15:11
[Import SAS Data (.sas7bdat)] #import #read #SAS #sas7bdat #haven
library(haven)
df <- read_sas("data.sas7bdat")
@sokarob
sokarob / Import Excel Files with readxl and openxlsx.r
Last active March 26, 2024 14:47
[Import Excel Files with readxl and openxlsx] #excel #xlsx #readxl #openxlsx #import #read
library(readxl) # or tidyverse
df <- readxl::read_excel('C:\\R\\Project Folder\\Spreadsheet.xlsx',sheet=NULL,range=NULL)
df <- readxl::read_excel('Spreadsheet.xlsx',sheet="Sheet1",range="C3:D5") #If working directory is set
df <- readxl::read_excel("Spreadsheet.xlsx", na="NA", col_names=FALSE) #If there is no header
library(openxlsx)
df <- read.xlsx("Spreadsheet.xlsx", sheet = 3, skipEmptyRows = TRUE)
df <- read.xlsx(string_variable, sheet = "Sheet1", skipEmptyRows = FALSE)
@sokarob
sokarob / Set Working Directory.r
Created March 22, 2024 13:10
[Set Working Directory] #wd #working_directory #directory #folder
#Set Working Directory between "" (use forward slashes instead of back slashes or use double back slashes):
wd <- "C:\\R\\Project Folder"
@sokarob
sokarob / Use Regular Expressions to Remove Characters.r
Created March 21, 2024 13:36
[Use Regular Expressions to Remove Characters] #regex #regular_expression #substitute #replace #gsub #NA
# Remove from strings that should consist only of numerals
df<-df %>% mutate("Column2"=gsub("\\D","",Column1))
# Use condition to replace fields that would be blank after removing the non-numeric characters with NA.
df<-df %>% mutate("Column2"=ifelse(gsub("\\D","",Column1)=="",NA,gsub("\\D","",Column1)))
# Remove everything that isn't alpha-numeric
df<-df %>% mutate("Column2"=gsub("[^a-zA-Z0-9]","",Column1))
# Use condition to replace fields that would be blank after removing the non-numeric characters with NA.
df<-df %>% mutate("Column2"=ifelse(gsub("[^a-zA-Z0-9]","",Column1)=="",NA,gsub("[^a-zA-Z0-9]","",Column1)))
@sokarob
sokarob / Prompt for User Input In Console With readline() Command From devtools.R
Created March 7, 2024 20:15
[Prompt for User Input In Console With readline() Command From devtools] #prompt #input #user_input #readline
library(devtools)
# ***************************************************
# *********** ENTER RESPONSE INTO CONSOLE! **********
# ***************************************************
x = readline(prompt = "Enter x:") # creates variable that can be used like any other string variable
# Credit to: https://www.geeksforgeeks.org/taking-input-from-user-in-r-programming/
# Note: There are validation tips and more details in this web page.
@sokarob
sokarob / Filter in Base R.r
Last active February 8, 2024 21:03
[Filter in Base R] #filter #base_R #date
# Character filter
df2 <- df1[df1$'column'=="string",]
# Numeric filter
df2 <- df1[df1$'column'>=1234,]
# Filter on date and additional condition:
df2 <- df1[df1$'date' >= "2023-12-01" & df1$'column'=="string",]