Data Utilities Flashcards
seq( ) function
Generates sequences, by specifying the from, to, and by arguments.
ex: seq(from = 2, to = 8, by = 2)
output: 2 4 6 8
rep( )
Replicate elements of vectors and lists
ex: rep(c(2,3,4), times = 3)
Output: 234234234
sort(x, decreasing = FALSE )
Sort a vector in ascending order. Works on numerics, but also on character strings and logicals.
sort(c(5,1,3))
output: 1 3 5
str( )
Display the structure of any R object.
append( )
Merge vectors or lists.
is.x
checks for the specified x class of an R object
is.character(“dog”)
TRUE
as.x
Convert an R object from one class to another
as.character(4)
“4”
grep( pattern, x )
Search for matches to argument “pattern” within each element of a character vector. returns the index of elements that match the pattern
ex: grep(pattern = “a”, c(“apple”, “dog”, “cat”))
return: 1 3
grepl( pattern, x)
Search for matches to argument “pattern” within each element of a character vector. Returns logicals based on whether each element matches the pattern:
ex: grepl(pattern = “a”, c(“apple”, “dog”, “cat”))
return: TRUE FALSE TRUE
Regular Expressions: ^
match characters that begin with whatever comes after the ^ sign
Regular Expressions: $
match characters at the end of a string
Regular Expressions: .*
matches any character (.) zero or more times (*). Both the dot and the asterisk are metacharacters. You can use them to match any character between the at-sign and the “.edu” portion of an email address.
”@.*\.edu$”
sub( pattern, replacement, x )
Uses the replace argument to replace the first match from the pattern argument
gsub( pattern, replacement, x)
Uses the replace argument to replace ALL matches from the pattern argument
Sys.Date( )
returns the current date. returns class “Date”