# setting up working directorygetwd()setwd()
data = read.csv("c:/Users/sshres07/Box/Teaching_2022/2022/Summer/HIA226/LectureSlides/Week1/Session01/Session01/moonPhase2019.csv")
setwd("c:/Users/sshres07/Box/Teaching_2022/2022/Summer/HIA226/LectureSlides/Week1/Session01/Session01")getwd()
data2 = read.csv("moonPhase2019.csv")
# Values of pi and exp functionpiexpexp(1)log(1)
# How do you account for missing when creating dataframes in rx=c(1,2,3)y=c("a","b", "c")z=c("aa", "bb")
new1 = data.frame(x,y,z)
x=c(1,2,3)y=c("a","b", "c")z=c("aa", "bb", NA)
new2 = data.frame(x,y,z)
# What are the common print commands
print(x)#This will give you an error why?
print(1)print("x")# This works
x=1print(x)
# once you assign a value of 1 to x then the print statement works
print("x")# But with this command you get x instead of 1. Why is that?
# printf is not widely used in r# it think it give you output that is formatted in C (another programming language)
################################### Substring ##########################################substring("abcedfgh", 1, 3)substring("abcedfgh", 2, 3)substring("abcedfgh", 3, 3)
############# Practice Problem: 1 ##############################################################################################################################################
# Create a string which has a structure xxx-xx-xxxx (x are all numbers)string= "444-43-4567"
# write substring code to extract last 4 digits of the stringslast4 = substring(string, 8, 12)
# Create a string which is your fullnamename = "Andrew Nguyen"
find the index of spaceindex of space + 1ending point length of string
# Extract your lastnamelastname =substring(name, 9, 17)lastname
################################ OMIT ############################################
x=c(1,2,3,4,NA,NA,NA,5)x
mean(x)
mean(x, na.rm= TRUE)
mean(na.omit(x))
x=na.omit(x)
################################ Adding new variable ##############################
names = c("Jon","Jane","Doe")gender = c("M","F","F")
dataset = data.frame(names,gender)
dataset$age = 5
#will add new variable
# it is not as simple to create a new row
# first you need to create a new variable
x = c("jim","F",5)
dataset = rbind(dataset, x)
dataset# Note age variable changes to string
######################### Practice problem 2#####################################################################################################################
names = c("Jon","Jane","Doe")gender = c("M","F","F")
dataset = data.frame(names,gender)
dataset$age = 5
# create a new variable which adds 10 to the age variable
dataset$age10 = + 10
############################################################################################################### Date and time #####################################
# 2022-01-20
x1 = as.Date("2022-01-20")x1
# But dates can be written in many different formats. So the format argument can be used to standardize the date field
# Jan20, 2022x2= as.Date("Jan20, 2022", format="%b%d,%Y")x2# January 20, 2022
x3 = as.Date("January 20,2022", format="%B %d, %Y")x3
# Okay but what are these %B %b %d %Y %y
# Lets look at the documentation# (https://www.ibm.com/docs/en/cmofm/9.0.0?topic=SSEPCD_9.0.0/com.ibm.ondemand.mp.doc/arsa0257.htm)
#################### Practice problems: 3 #####################################################################################################################
# 20 Jan 2022p1 = as.Date()
# 20022022p2 = as.Date()
# 2/20/2022p4 = as.Date()
# Jan 20, 22p5 = as.Date()
# Create a variable DOB (your date of birth or a random date)dob = as.Date("YYYY-MM-DD")
today = as.Date("YYYY-MM-DD")
yourageyears = difftime()
yourageweeks = difftime()
youragedays = difftime()
################################################################################