More details could be found in the html file here
Important points about text in data set
Variables with caracter values
Step 1: Fixing charactre vectors topupper
and tolower
functions.
if(!file.exists("./data")) dir.create("./data")
fileUrl <- "https://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv?accessType=DOWNLOAD"
download.file(fileUrl, destfile = "./data/cameras.csv")
cameraData <- read.csv("./data/cameras.csv")
names(cameraData)
tolower(names(cameraData))
strsplit
function. splitNames <- strsplit(names(cameraData), "\\.")
splitNames[[5]]
splitNames[[6]]
lists
myList <- list(letters = c("A", "b", "c"), numbers = 1:3, matrix(1:15, 5))
head(myList)
sapply
splitNames[[6]][1]
firstElement <- function(x) x[1]
sapply(splitNames, firstElement)
if(!file.exists("./data")) dir.create("./data")
# download data set
fileUrl1 <- "https://dl.dropbox.com/u/7710864/data/reviews-apr29.csv"
fileUrl2 <- "https://dl.dropbox.com/u/7710864/data/solutions-apr29.csv"
download.file(fileUrl1, destfile = "./data/reviews.csv")
download.file(fileUrl2, destfile = "./data/solution.csv")
# load data set
reviews <- read.csv("./data/reviews.csv")
solutions <- read.csv("./data/solution.csv")
# view data set
head(reviews, 2)
head(solutions, 2)
sub()
(replace the first match)names(reviews)
sub("_", "", names(reviews))
gsub()
(replace globally)testName <- "this_is_a_test"
sub("_", "", testName)
gsub("_", "", testName)
grep()
and grepl()
functionsgrep("Alameda", cameraData$intersection) # return index
table(grepl("Alameda", cameraData$intersection)) # return true or false
cameraData2 <- cameraData[!grepl("Alameda", cameraData$intersection), ]
grep()
grep("Alameda", cameraData$intersection, value = TRUE) # retrun names containing "Aladema"
grep("JeffStreet", cameraData$intersection)
length(grep("JeffStreet", cameraData$intersection))
library(stringr)
nchar("Jeffrey Leek")
substr("jeffrey Leek", 1, 7)
paste("Jeffrey", "Leek")
paste0("Jeffrey", "Leek")
str_trim("Jeff ")
Regular expressions:
grep, grepl, regexpr, gregexpr, sub, gsub
and strsplit
.Most characters, including all letters and digits, are regular expressions that match themselves. Any metacharacter with special meaning may be quoted by preceding it with a backslash. The metacharacters in extended regular expressions are . \ | ( ) [ { ^ $ * + ?
, but note that whether these have a special meaning depends on the context.
Positions
^
matches the begining.$
matches the end.\b
matches the empty string at either edge of a word.\B
matches the empty string provided it is not at an edge of a word.Quantifiers
*
matches at least 0 times.+
matches at least 1 times.?
matches at most 1 times.{m}
matches exactly m times.{m.}
matches at least m times.{n, m}
matches between n to m times.Others:
[ ]
matches any character appearing in []
. ex: [a-z]
[^ ]
matches any character not appearing in [ ]
..
matches any character.|
matches alternative metacharacters.\
suppress the special meaning of metacharacters in regular expression.()
groups expression.Character classes:
[:digit:]
or \d
equivalent to [0-9]
.[:lower:]
equivalent to [a-z]
.[:upper:]
equivalent to [A-Z]
.[:alpha:]
equivalent to [a-zA-Z]
or [[:lower:][:upper:]]
.[:alnum:]
equivalent to [A-z0-9]
or [[:digit:][:alpha:]]
.\w
equivalent to [[:apnum]_]
or [A-z0-9_]
.\W
equivalent [^A-z0-9]
.[:xdigit:]
matches 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f
.[:blank:]
matches space or tab.[:space:]
marches tab, newline, vertical tab, form feed, carriage return, space.\s
space ” “.\S
not space.[:punct]
matches ! " # $ % & ’ ( ) * + , - . / : ; < = > ? @ [ ] ^ _ ` { | } ~
.[:graph:]
equivalent to [[:alnum:][:punct:]]
.[:print:]
equivalent to [[:alnum:][:punct:]\\s]
.[:cntrl:]
control characters, like \n
or \r
, [\x00-\x1F\x7F]
.R function summary:
grep(..., value = FALSE)
, grepl()
, stringr::str_detect()
.grep(..., value = TRUE)
, stringr::str_extract()
, stringr::str_extract_all()
.regexpr()
, gregexpr()
, stringr::str_locate()
, string::str_locate_all()
.sub()
, gsub()
, stringr::str_replace()
, stringr::str_replace_all()
.strsplit()
, stringr::str_split()
.date()
returns a character that gives you the date and time. d1 <- date()
d1
class(d1)
d2 <- Sys.Date()
d2
class(d2)
%d
= days as number(0-31).%a
= abbreviated weekday.%A
= unabbreviated weekday.%m
= month(00-12).%b
= abbreviated month.%B
= unabbreviated month.%y
= 2 digit year.%Y
= 4 digit year.format(d2, "%a %b %d")
# if returns NA, please use
lct <- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME", "C")
x <- c("1jan1960", "2jan1960", "31mar1960", "30Jul1960")
z <- as.Date(x, "%d%b%Y")
z
z[1] - z[2]
as.numeric(z[1] - z[2])
weekdays(d2)
months(d2)
julian(d2)
lubridate
package.library(lubridate)
ymd("20140108")
mdy("08/04/2013")
dmy("03-04-2013")
ymd_hms("2011-08-03 10:15:03")
ymd_hms("2011-08-03 10:15:03", tz = "Pacific/Auckland")
x <- dmy(c("1jan2013", "2jan2013", "31mar2013", "30Jul2013"))
wday(x[1])
wday(x[1], label = TRUE)
ymd("1989 May 17")
mdy("March 12 1975")
dmy(25081985)
ymd("1920/1/2")
ymd_hms(now())
hms("03:22:14")
dt2 <- c("2014-05-14", "2014-09-22", "2014-07-11")
ymd(dt2)