Download presentation
Presentation is loading. Please wait.
Published byCecily Tucker Modified over 9 years ago
1
Actor Heights 1)Create Vectors of Actor Names, Heights, Date of Birth, Gender 2) Combine the 4 Vectors into a DataFrame
2
Numeric: e.g. heights String: e.g. names Dates: “12-03-2013 Factor: e.g. gender Boolean: TRUE, FALSE Variable Types
3
We use the c() function and list all values in quotations so that R knows that it is string data. Create a variable called ActorNames as follows: ActorNames <- c(“John", “Meryl”, “Jennifer", “Andre") Creating a Character / String Vector
4
Class, Length, Index class(ActorNames) length(ActorNames) ActorNames[2]
5
Create a variable called ActorHeights (inches): ActorHeights <- c(77, 66, 70, 90) Creating a Numeric Vector / Variable
6
Use the as.Date() function: ActorDoB <-as.Date(c("1930-10-27", "1949-06-22", "1990-08-15", "1946-05-19“ )) Each date has been entered as a text string (in quotations) in the appropriate format (yyyy-mm-dd). By enclosing these data in the as.Date() function, these strings are converted to date objects. Creating a Date Variable
7
Use the factor() function: ActorGender <- c(“male", “female", “female", “male“ ) ActorGender <- factor(ActorGender) Creating a Categorical / Factor Variable
8
Actor.DF <-data.frame(Name=ActorNames, Height=ActorHeights, BirthDate = ActorDob, Gender=ActorGender) Vectors and DataFrames dim(Actor.DF) Actor.DF[2] Actor.DF[2,] Actor.DF[1,3] Actor.DF[2,2] Actor.DF[2:3,]
9
> getwd() [1] "C:/Users/johnp_000/Documents" > setwd() getwd() setwd()
10
write.table(Actors.DF, “ActorData.txt", sep="\t", row.names = TRUE) write.csv(Actors.DF, “ActorData.csv") Write / Create a File
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.