Skip to content
Course Notes: Intermediate R
add text here
# If-Statement
if (condition) {
expr1
} else {
expr2
}
#It's important that the else keyword comes on the same line as the closing bracket of the if part!
if (condition1) {
expr1
} else if (condition2) {
expr2
} else if (condition3) {
expr3
} else {
expr4
}# While Loop
while (condition) {
expr
}
while(ctr <= 7){
if(ctr %% 5 == 0){
break
}
print("Hello!")
ctr <- ctr + 1
}#Mathematische Funktionen
##abs(): Calculate the absolute value.
##sum(): Calculate the sum of all the values in a data structure.
##mean(): Calculate the arithmetic mean.
##round(): Round the values to 0 decimal places by default. Try out ?round in the console for variations of round() and ways to change the number of digits to round to.
#Funktionen für die Analyse von Datenstrukturen
##seq(): Generate sequences, by specifying the from, to, and by arguments.
##rep(): Replicate elements of vectors and lists.
##sort(): Sort a vector in ascending order. Works on numerics, but also on character strings and logicals.
##rev(): Reverse the elements in a data structures for which reversal is defined.
##str(): Display the structure of any R object.
##append(): Merge vectors or lists.
##is.*(): Check for the class of an R object.
##as.*(): Convert an R object from one class to another.
##unlist(): Flatten (possibly embedded) lists to produce a vector.
#grepl() und greP()
##grepl(), which returns TRUE when a pattern is found in the corresponding character string.
##grep(), which returns a vector of indices of the character strings that contains the pattern.
#^,$ bereits bekannt (was die im pattern machen), es gibt aber auch
##@, because a valid email must contain an at-sign.
##.*, which 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$, to match the ".edu" part of the email at the end of the string. The \\ part escapes the dot: it tells R that you want to use the . as an actual character.
#While grep() and grepl() were used to simply check whether a regular expression could be matched with a character vector, sub() and gsub() take it one step further: you can specify a replacement argument. If inside the character vector x, the regular expression pattern is found, the matching element(s) will be replaced with replacement. sub() only replaces the first match, whereas gsub() replaces all matches.
#.*: A usual suspect! It can be read as "any character that is matched zero or more times".
#\\s: Match a space. The "s" is normally a character, escaping it (\\) makes it a metacharacter.
#[0-9]+: Match the numbers 0 to 9, at least once (+).
#([0-9]+): The parentheses are used to make parts of the matching string available to define the replacement. The \\1 in the replacement argument of sub() gets set to the string that is captured by the regular expression [0-9]+.
#In R, dates are represented by Date objects, while times are represented by POSIXct objects. Under the hood, however, these dates and times are simple numerical values. Date objects store the number of days since the 1st of January in 1970. POSIXct objects on the other hand, store the number of seconds since the 1st of January in 1970.
#as.Date("13 January, 1982", format = "%d %B, %Y")
#unclass(my_date1)
#format(Sys.Date(), format = "Today is a %A!")
#?strptime full list of conversion symbols of time