Concept Flow - Character (string) type
Create character variable
Store text value
Use or print variable
End or reuse variable
This flow shows how a character (string) variable is created, stored, used, and then either ends or is reused.
name <- "Alice" print(name) name <- "Bob" print(name)
| Step | Action | Variable 'name' Value | Output |
|---|---|---|---|
| 1 | Assign 'Alice' to name | "Alice" | |
| 2 | Print name | "Alice" | [1] "Alice" |
| 3 | Assign 'Bob' to name | "Bob" | |
| 4 | Print name | "Bob" | [1] "Bob" |
| 5 | End of code | "Bob" |
| Variable | Start | After Step 1 | After Step 3 | Final |
|---|---|---|---|---|
| name | undefined | "Alice" | "Bob" | "Bob" |
Character (string) type in R: - Use quotes to create strings, e.g. name <- "Alice" - Strings are stored as text values - Print shows text with quotes and index - Variables can be reassigned new strings - Strings are used for text data
"hello" uses quotes correctly to create a character string."Hello" and "World" in R?paste() is used to join strings with a space by default.paste("Hello", "World") uses the correct function paste() to join strings.text <- "R is fun" substr(text, 1, 3)
words <- c(hello, "world", "R")
hello is unquoted.hello causes R to look for a variable named hello, which likely does not exist.hello -> Option Dc("apple", "banana", "cherry"). How do you create a single character string with these words separated by commas and a space, like "apple, banana, cherry"?collapse argument joins vector elements into one string separated by the given string.paste(c("apple", "banana", "cherry"), collapse=", ") correctly uses paste() with collapse=", " to join words with comma and space.sep controls separator between arguments, not vector elements; collapse is needed here.Pass the test and this pattern moves unknown