CSC121 - R Console Monday January 29th --------------------------- > # STRINGS > > # We're going to learn about a new type of data > # This is data that is made up of characters > courseName <- "csc121" > courseName [1] "csc121" > # This type of data is calleda 'string' > # String values should be enclosed with double quotes > # single quotes will work too, but it is common to have words like "can't" or "won't" that have single quotes, and you might close the string before you actually wanted to > # so better to always use double quotes > typeof(courseName) [1] "character" > # The type of a string in R is 'character' because strings are made up of characters > # You can make long strings: > s <- "This is a very long sentence that needs to span many many many many many many lines (really just 2)" > s [1] "This is a very long sentence that needs to span many many many many many many lines (really just 2)" > > # Concatenating strings together to make a new string > # To put two or more strings together (concatenating), you can use the 'paste'' function > s1 <- "ice" > s2 <- "cream" > paste(s1, s2) [1] "ice cream" > paste("I", "love", "R") [1] "I love R" > # Notice that paste creates a *new* string with spaces between each word > # But you can add an argument to paste to change the separation > # NEW way of giving an argument in a function call: Stating the name of the argument in the function call: > paste(s1, s2, sep = ",") [1] "ice,cream" > # 'sep' is the name of the argument, and "," is the value we're giving the argument > # For most functions you write, you shouldn't worry about calling your function like this > # this is usually used for functions like paste that can take 'unlimited' amounts of strings to put together and you need to tell it when the argument is used as the string separator > > # Empty string: > emptyString <- "" > emptyString [1] "" > # This string has no characters > # It can also be used as a separator > paste(s1, s2, sep = "") [1] "icecream" > paste(s1, s2, sep = emptyString) [1] "icecream" > > # You can also extract part of a string: > # Example: Get the 4th through 6th characters of the string "csc121" > # use the function 'substring' > substing("csc121", 4, 6) Error in substing("csc121", 4, 6) : could not find function "substing" > substring("csc121", 4, 6) [1] "121" > # Get the first character: > substring("csc121", 1, 1) [1] "c" > # Getting everything from the second character onwards: > substring("csc121", 2) [1] "sc121" > # When you don't give an argument for when the string should end, R uses the length of the string as a default argument (another way to call functions, which you don't have to worry about for your functions right now) > > # Speaking of length of a string, how do we find it? > # let's try our length function: > courseName [1] "csc121" > lenght(courseName) Error in lenght(courseName) : could not find function "lenght" > length(courseName) [1] 1 > # Why did we get 1? > # Strings also go into vectors! > # One string is just a vector with one string element > # To get the length of a string, what we really want is the number of characters in that string > nchar(courseName) [1] 6 > s [1] "This is a very long sentence that needs to span many many many many many many lines (really just 2)" > nchar(s) [1] 99 > emptyString [1] "" > nchar(emptyString) [1] 0 > > # So let's talk more about these string vectors > # We can create a strgin vector using c(...) > c("i", "love", "R") [1] "i" "love" "R" > sentence <- c("i", "love", "R") > sentence [1] "i" "love" "R" > # Can create a string vector full of empty strings: > character(5) [1] "" "" "" "" "" > logicial(5) Error in logicial(5) : could not find function "logicial" > logical(5) [1] FALSE FALSE FALSE FALSE FALSE > numeric(5) [1] 0 0 0 0 0 > # Empty string vector (NOT an empty string) > character(0) character(0) > length(character(0)) [1] 0 > > # Type conversion between string and numeric > as.character(53) [1] "53" > # as.character returns the string representation of the argument > as.character(25.43) [1] "25.43" > as.double("53") [1] 53 > # as.double gives us back the numeric representation > as.double("fdhfjh") [1] NA Warning message: NAs introduced by coercion > "3" + 5 Error in "3" + 5 : non-numeric argument to binary operator > as.double("3") + 5 [1] 8 > # Must convert to a number first to add > # What happens when we try to combine strings and numbers in a vector? > c(2, 4, "hello") [1] "2" "4" "hello" > c(2, 4, "36") [1] "2" "4" "36" > > # String Comparisons > # We know how to compare numbers: 4 < 6 > # Just like we used comparison operators on numbers, we can use them on strings as well: > "apple" < "banana" [1] TRUE > # Strings are compared by dicitonary/alphabetical order > "a" < "b" [1] TRUE > # Comparisons are case sensitive > "a" == "A" [1] FALSE > # If the letter is the same, lowercase letters come before uppercase > "a" < "A" [1] TRUE > # ..but not if they are different letters > "b" < "A" [1] FALSE > "." < "," [1] FALSE > # You can play around with these to see what is considered 'more than' or 'less than' > > # Reading input from the user > # R has a function that allows you to ask the user for an input and save it to a variable > # 'readline' > favouriteIceCream <- readline("What is your favourite ice cream flavour?: ") What is your favourite ice cream flavour?: Strawberry > favouriteIceCream [1] "Strawberry" > favouriteIceCream <- readline("What is your favourite ice cream flavour?: ") What is your favourite ice cream flavour?: Chocolate > favouriteIceCream [1] "Chocolate" > # This is useful when you want to work with the user data in real time > # Notice that this pauses the program flow > # Until the user types something in, the program does not continue > roomNumber <- readline("What room are we in right now?: ") What room are we in right now?: WB 116 > roomNumber [1] "WB 116" > > # String splitting > # You can take a string and split it up into parts and put those parts in a vector > # Example: split the string "i love R" on the space characters > space <- " " > space [1] " " > strsplit("i love R", " ") [[1]] [1] "i" "love" "R" > # Notice the [[1]] with double square brackets > # String splitting returns an R type that we will look at more closely later called a 'list' > h <- strsplit("i love R", " ") > typeof(h) [1] "list" > # For now, all you need to know is how access that vector in the list > h [[1]] [1] "i" "love" "R" > # how do we get that vector? > h[[1]] [1] "i" "love" "R" > words <- h[[1]] > words [1] "i" "love" "R" > # We can split on any character, like a comma > numbers <- strsplit("one,two,three", ",") > numbers [[1]] [1] "one" "two" "three" > numbers[[1]] [1] "one" "two" "three" > # Remember to index the list with [[1]] > > words [1] "i" "love" "R" > words[1] [1] "i" > words[2] [1] "love" > words[3] [1] "R" > > # One more new function: 'cat' > # 'cat' combines strings together for printing to the console > cat("i", "love", "r") i love r > # This is not the same as 'paste', which returns a vector > paste("i", "love", "r") [1] "i love r" > # All cat does is print to the console > # This is used for formatting strings together so that they look nice when you print them > # Newline character: '\n' > # This is the equivalent of pressing enter on your keyboard > cat("This is the first sentence\n", "This is the second sentence") This is the first sentence This is the second sentence > > # Let's combine some things we learned into a function > source('~/parrot.R') > > > Parrot() What did you say?: Hello! Hello! Hello! Hello! Hello! > source('~/parrot.R') > Parrot() What did you say?: HELLO HELLOHELLO HELLO HELLO > source('~/parrot.R') > Parrot() What did you say?: HIIIIII HIIIIII HIIIIII HIIIIII HIIIIII > source('~/parrot.R') > Parrot() What did you say?: Hey Hey This is on a new line Hey Hey Hey