#SPSP Introduction to R #Video 2 Script #Numeric and Integer Objects---- #Assign numbers 1, 2, and 3 to object PartID PartID <- c(1,2,3) PartID # #Assign numbers 1, 2, and 3 to object PartID2 PartID2 <- c(1:3) PartID2 # #Assign two heights (64.5, 70) to object Ht Ht <- c(64.5, 70) # #Assign heights from Ht plus new height of 65 to Ht2 Ht2 <- c(Ht, 65) # #Check the mode/type of each object created class(PartID) class(PartID2) class(Ht2) # #Convert PartID to integer type PartIDb <- as.integer(PartID) class(PartIDb) # #Convert PartID2 to numeric type PartID2b <- as.numeric(PartID2) class(PartID2b) # # #Character and Factor Objects---- #Assign genders for three individuals to Gnd object Gnd <- c("woman","man","woman") Gnd class(Gnd) # #Convert charactet object to factor #and assign to object Gnd2 Gnd2 <- factor(Gnd) Gnd2 class(Gnd2) #Check length of Gnd2 length(Gnd2) # #Assign data to object with NA (missing) included Gnd3 <- c("woman","man","woman", NA) Gnd3 #See total length of Gnd3 length(Gnd3) #See length with NA (missing) omitted length(na.omit(Gnd3)) # #Generate frequency table for gender table(Gnd3) # #Count number of NA responses sum(is.na(Gnd3)) # #See objects in environment ls() #Remove objects from environment rm(Gnd, Gnd3, Ht, PartID, PartID2b, PartIDb) # # #Working with Logical Statements---- #Logical test: #Is response on Gnd2 equal to woman? Gnd2=="woman" #Sum output from logical test: #How many times does response woman appear in Gnd2? sum(Gnd2=="woman") #Sum output using OR: #How many times does woman OR man appear in Gnd2? sum(Gnd2=="woman"|Gnd2=="man") #Does response woman appear in Gnd2? "woman" %in% Gnd2 # #Logical with Continuous #Look at Ht2 object Ht2 #Which elements in Ht2 are equal to 65? Ht2==65 #Which elements in Ht2 are greater than 65? Ht2>65 #Which elements in Ht2 are less than 65? Ht2<65 #Which elements in Ht2 are less than or equal to 65? Ht2<=65 #Which elements in Ht2 are NOT equal to 65? Ht2!=65 # #Vectors vs Lists---- #Combine objects using c #Objects will be coerced to be same type dataList <- c(PartID2, Gnd2, Ht2) dataList class(dataList) # #Combine objects using list #Objects retain original types dataList2 <- list(PartID2, Gnd2, Ht2) dataList2 class(dataList2) class(dataList2[[2]]) # #Combine objects into data frame: GndHt_DF GndHt_DF <- data.frame(PartID2, Gnd2, Ht2) View(GndHt_DF) #Check dimensions of data frame dim(GndHt_DF) # #Vectorized function---- #Add 20 to each element in PartID2 column #from GndHt_DF data frame GndHt_DF$PartIDnew <- GndHt_DF$PartID2+20 View(GndHt_DF) # #Create z Scores for Height #Save mean and standard deviation for height meanHt <- mean(GndHt_DF$Ht2) meanHt sdHt <- sd(GndHt_DF$Ht2) sdHt #Use mean and sd to calculate z score #for height for each element in Ht2 column GndHt_DF$zHt <- (GndHt_DF$Ht2-meanHt)/sdHt View(GndHt_DF) # #Working with Data---- #Select columns 2 through 5 #from GndHt_DF and assign to GndHt_DF2 GndHt_DF2 <- GndHt_DF[,2:5] View(GndHt_DF2) #Select all columns from GndHt_DF #EXCEPT column 1 and assign to GndHt_DF2b GndHt_DF2b <- GndHt_DF[,-1] View(GndHt_DF2b) #Assign columns from GndHt_DF to #GndHt_DF2c in new order 3rd, 1st, 2nd, 4th GndHt_DF2c <- GndHt_DF[,c(4, 2:3, 5)] View(GndHt_DF2c) #Label columns Var1, Var2, Var3, Var4 names(GndHt_DF2c) <- paste("Var", 1:4, sep="") View(GndHt_DF2c) #Label columns ID, Gender, Height, zHeight names(GndHt_DF2c) <- c("ID","Gender","Height", "zHeight") View(GndHt_DF2c) # #Select cases (rows) that meet criteria #Logical test with two variables: #Count number rows with women who are 65 or taller sum(GndHt_DF2c$Gender=="woman" & GndHt_DF2c$Height>=65) #Identify rows with women who are 65 or taller GndHt_DF2c$Gender=="woman" & GndHt_DF2c$Height>=65 #Assign rows with women who are 65 or taller #to new object: WomenGT65ht WomenGT65ht <- GndHt_DF2c[GndHt_DF2c$Gender=="woman" & GndHt_DF2c$Height>=65,] View(WomenGT65ht)