This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df2 <- aggregate(df$points, by=list(df$team, df$position), FUN=mean) | |
#credit to https://www.statology.org/aggregate-r/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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']] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(haven) | |
df <- read_sas("data.sas7bdat") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Set Working Directory between "" (use forward slashes instead of back slashes or use double back slashes): | |
wd <- "C:\\R\\Project Folder" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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))) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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",] |
NewerOlder