Writing

codes i especially like———————————————–

packages – run all of my library commands at once:———————

library(pacman)
pacman::p_load(dplyr, tidyverse, ggplot2, tidyr, knitr, here, lubridate,
Rcpp, googlesheets4, faraway, mosaic, av,
gifski, gganimate, png, janitor, skimr, swirl)

directories, files, etc———————————————–

getwd()

example:

setwd(“D:/R_shared/rebuild”)
bact <- read.csv(“K’avi Bacteria and Nutrient Data.csv”)

often read.csv does not work for me. i dont know why. but read_csv(here… does.

via “here” package:

bact <- read_csv(here(“data”, “K’avi Bacteria and Nutrient Data.csv”))

this is another way to tell R where to find my file, from where it understands

my working directory to currently be.

MAKING A F_AROUND TABLE:

Fbact <- read_csv(here(“data”, “K’avi Bacteria and Nutrient Data.csv”))

export a csv file from a table i made————————————–

write_csv(bact, “bact.csv”)

quick looks, temporary——————————————————

this lets me look temporary at one column

bact$date

to look at if columns are chr, int, num, etc, either of these:

glimpse(bact)
str(bact)

to look at stat summary, quartiles, etc:

summary(bact)

date stuff————————————————————

this is a multi step process. firts,

this changes the format of my date column. look at date before i run.

then look at it after, to see the change.

im not sure why but the command that comes

after this will not work unless i do this one first:

bact$date<- mdy(bact$date)

this one seems to a new column for year, and previous date format command stays

bact$year <- year(bact$date)

note: Rladies sydney have another, similar command. i dont recall what

it is right now. put it here when i find it.

Fbact$date<-mdy(Fbact$date)
Fbact$year<-year(bact$date)

delete column———————————————————

first, look at current column names:

colnames(bact)

console reads:

[1] “…1” “sample_location” “longitude”

[4] “latitude” “date” “E coli col/100 mL”

[7] “Total Coliform col/100 mL” “Nitrates mg/L” “mg/L”

[10] “Turbidity ntu” “year”

(i think) this code tells R, look at column 1, delete it.

and since the name is reassigned, it makes it permanent:

bact<-bact[,-1]
Fbact<-Fbact[,-1]

rename column—————————————————–

changes col names, all at once, permanent:

colnames(bact)<-c(“sample_location”, “longitude”, “latitude”, “date”,
“e_coli”, “tot_coliform”, “nitrate”, “unknown”,
“turbidity”, “year”)

col rename via Rladies example cleanitup L1V2:

cleanbeaches <- rename(cleanbeaches, bacteria = enterococci_cfu_100ml)

same code, 2nd example – rename Fbact example:

Fbact<-rename(Fbact, e_coli = “E coli col/100 mL”)

another example of all-at-once:

colnames(Fbact)<-c(“sample_location”, “longitude”, “latitude”, “date”,
“e_coli”, “tot_coliform”, “nitrate”, “unknown”,
“turbidity”, “year”)

column order change—————————————————

i can chose my order, and also say ‘everything()’ to leave the rest, after

my preferred order:

cleanbeaches<-select(cleanbeaches, council, site, bacteria, everything())

Fbact example:

Fbact<-select(Fbact, sample_location, date, e_coli, everything())
names(Fbact)

change it back, same way:

Fbact<-select(Fbact, sample_location, longitude, latitude, date, e_coli,
everything())

desc-ascend column arrange table,————————————————

temporary example:

Fbact %>% arrange(desc(e_coli))

Or, this could be done via a similar command, same result:

cleanbeaches %>% arrange(-bacteria)

isolate a column————————————————————

to make a table from only one column:

cleanbeaches %>% filter(site == “Coogee Beach”)

example 2, as a permanent table (for fun,a descending arrange added):

FbactSite4 <- Fbact %>% filter(sample_location == “Monitoring Site 4”) %>%
arrange(-e_coli)

piping examples————————————————————–

pipes let me run several commands at one time.

here i am ‘grouping by’ both sample location and year.

also, i am asking for particular stat info:

bact_sum <- bact %>%
group_by(sampe_location, year) %>%
summarize(mean_ecoli = mean(e_coli, na.rm = T),
median_ecoli = median(e_coli, na.rm = T),
min_ecoli = min(e_coli, na.rm = T),
max_ecoli = max(e_coli, na.rm = T),
mean_tot_col = mean(tot_coliform, na.rm = T),
median_tot_col = median(tot_coliform, na.rm = T),
min_tot_col = min(tot_coliform, na.rm = T),
max_tot_col = max(tot_coliform, na.rm = T))

i can look at bact and bact_sum to see how the ‘group by’ turned out.

also, its a whole different sort of chart. it give stat summaries, by year and site.

join 2 tables——————————————————————

first, call up my kavi clean water data AND name it “water”:

water <- read_csv(here(“data”, “K’avi Tribe Water Quality Dataset_clean.csv”))

this command puts water and bact into same table:

join<-full_join(water, bact)

if an issue pops up with the way the date is in the water table i could run this code,

then try the join code again. for me, it worked the first time so i dont need to run:

water$date<-ymd(water$date)
join<-full_join(water, bact)

f-around and find out—————————————————–

i wanted to take out that date column that was part of the water table:

join<-join[,-5]
join<-join[,-1]

%d bloggers like this: