Notebook of Reading Books: R in Action_Chapter 4.

This chapter covers

  • Manipulating dates and missing values

  • Understanding data type conversions

  • Creating and recoding variables

  • Sorting, merging, and subsetting datasets

  • Selecting and dropping variables

Attach is the Script of chapter4.

Show me the code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Remove most objects from the working environment
rm(list = ls())
options(stringsAsFactors = F)

# 4.1. A working example
# code listing 4.1. Creating the leadership data frame
manager <- c(1, 2, 3, 4, 5)
date <- c("10/24/08", "10/28/08", "10/1/08", "10/12/08", "5/1/09")
country <- c("US", "US", "UK", "UK", "UK")
gender <- c("M", "F", "F", "M", "F")
age <- c(32, 45, 25, 39, 99)
q1 <- c(5, 3, 3, 3, 2)
q2 <- c(4, 5, 5, 3, 2)
q3 <- c(5, 2, 5, 4, 1)
q4 <- c(5, 5, 5, NA, 2)
q5 <- c(5, 5, 2, NA, 1)
leadership <- data.frame(manager, date, country, gender, age,
                         q1, q2, q3, q4, q5, stringsAsFactors=FALSE)


# 4.2. Creating new variables
# code listing
mydata <- data.frame(x1 = c(2, 2, 6, 4),
                   x2 = c(3, 4, 2, 8))

mydata$sumx  <-  mydata$x1 + mydata$x2
mydata$meanx <- (mydata$x1 + mydata$x2)/2

attach(mydata)
mydata$sumx  <-  x1 + x2
mydata$meanx <- (x1 + x2)/2
detach(mydata)

mydata <- transform(mydata,
                    sumx  =  x1 + x2,
                    meanx = (x1 + x2)/2)
head(mydata)

# 4.3. Recoding variables
within()

# code
leadership <- within(leadership,{
  agecat <- NA
  agecat[age > 75]              <- "Elder"
  agecat[age >= 55 & age <= 75] <- "Middle Aged"
  agecat[age < 55]              <- "Young" })
# 4.4. Renaming variables
fix()

library(plyr)
rename()

# 4.5. Missing values
# NA, not available

is.na(leadership[,6:10])

# 4.5.2. Excluding missing values from analyses
x <- c(1, 2, NA, 3)
y <- sum(x, na.rm=TRUE)
y

# Deleting all observations with missing data (called listwise deletion)
na.omit()

newdata <- na.omit(leadership)
head(newdata) # row 4th containing missing data is deleted.

# 4.6. Date values
as.Date()

mydates <- as.Date(c("2007-06-22", "2004-02-13"))
mydates

str(leadership)
myformat <- "%m/%d/%y"
leadership$date <- as.Date(leadership$date, myformat)
leadership$date
head(leadership)

Sys.Date()
# [1] "2020-10-22"

date()
# [1] "Thu Oct 22 09:46:54 2020"

today <- Sys.Date()
format(today, format="%B %d %Y")
# [1] "October 22 2020"
format(today, format="%A")
# [1] "Thursday"

today <- Sys.Date()
dob <- as.Date("2015-07-09")
difftime(today, dob, units = "weeks")

# Test the day in the week, same with today
difftime(today, dob, units = "days" )/7
# Time difference of 276 days

format(today, format="%A")
# [1] "Thursday"

# 4.7. Type conversions
# Test
is.numeric()
is.character()
is.vector()
is.matrix()
is.data.frame()
is.factor()
is.logical()

# Convert
as.numeric()
as.character()
as.vector()
as.matrix()
as.data.frame()
as.factor()
as.logical()

# 4.8. Sorting data
order()

# 4.9. Merging datasets

# 4.9.1. Adding columns horizontally
merge()
# by one or more common key variables
total <- merge(dataframeA, dataframeB, by="ID")

# without common key variables
total <- cbind(A, B)

# 4.9.2. Adding rows vertically
total <- rbind(dataframeA, dataframeB)

# 4.10. Subsetting datasets
# 4.10.1. Selecting (keeping) variables
myvars <- paste("q", 1:5, sep = "")
myvars
head(myvars)

# 4.10.2. Excluding (dropping) variables
# method1
myvars <- names(leadership) %in% c("q3", "q4")
newdata <- leadership[!myvars]

# equal method2
newdata <- leadership[c(-8,-9)]

# alternative method
leadership$q3 <- leadership$q4 <- NULL

# 4.10.3. Selecting observations

# code listing 4.6
newdata <- leadership[1:3,]

newdata <- leadership[which(leadership$gender=="M" &
                              leadership$age > 30),]

attach(leadership)
newdata <- leadership[which(gender=='M' & age > 30),]
detach(leadership)

# 4.10.4. The subset() function

# 4.10.5. Random samples
mysample <- leadership[sample(1:nrow(leadership), 3, replace=FALSE),]

# 4.11. Using SQL statements to manipulate data frames
# code listing 4.7
library(sqldf)
newdf <- sqldf("select * from mtcars where carb=1 order by mpg",
               row.names=TRUE)
head(newdf)


sqldf("select avg(mpg) as avg_mpg, avg(disp) as avg_disp, gear
      from mtcars where cyl in (4, 6) group by gear")