This is part of My notes on R programming on my site https://dataz4s.com
# Read in data via read_excel
library(readxl)
LungCapData <- read_excel("C:/Users/Usuario/Documents/dataZ4s/R/MarinLectures/LungCapData.xlsx",
col_types = c("numeric", "numeric", "numeric",
"text", "text", "text"))
# R reads in Smoke, Gender and Caesarean as "text". Needs change to "factor"
# Change Smoke, Gender and Caesarean to factors with as.factor() command
LungCapData$Smoke <- as.factor(LungCapData$Smoke)
LungCapData$Gender <- as.factor(LungCapData$Gender)
LungCapData$Caesarean <- as.factor(LungCapData$Caesarean)
# attach(LungCapData)
attach(LungCapData)
names(LungCapData)
## [1] "LungCap" "Age" "Height" "Smoke" "Gender" "Caesarean"
# See what working directory we are in
getwd()
## [1] "C:/Users/Usuario/Documents/dataZ4s/R/WD"
# Setting wd by inserting path
setwd("C:/Users/Usuario/Documents/dataZ4s/R/WD")
# Making use of the ~ command
setwd("~/dataZ4s/R/WD")
# Saving path into an object can make it easier as it can be saved in script
# Enables to set wd via code in e.g. a script
projectWD <- "/~/dataZ4s/R/WD"
# Enables to set wd via setwd(projectWD)
# Say we wish save some work
# We will start creating some work
MeanAge <- mean(Age)
x <- c(1,2,3,4,5)
y <- 14
z <- summary(LungCapData)
z
## LungCap Age Height Smoke Gender
## Min. : 0.507 Min. : 3.00 Min. :45.30 no :648 female:358
## 1st Qu.: 6.150 1st Qu.: 9.00 1st Qu.:59.90 yes: 77 male :367
## Median : 8.000 Median :13.00 Median :65.40
## Mean : 7.863 Mean :12.33 Mean :64.84
## 3rd Qu.: 9.800 3rd Qu.:15.00 3rd Qu.:70.30
## Max. :14.675 Max. :19.00 Max. :81.80
## Caesarean
## no :561
## yes:164
##
##
##
##
# Save project
save.image("FirstProject.Rdata")
# To load the project we can use the load() command
This is a walk through of Mick Marin’s Statslectures video ‘Setting Up WD in R’