Concept Flow - Numeric and character vectors
Start
Create vector
Check type: numeric or character?
Store numbers
Use vector
We start by creating a vector, then check if it holds numbers or text, and store accordingly for use.
nums <- c(10, 20, 30) chars <- c("a", "b", "c") print(nums) print(chars)
| Step | Action | Variable | Value | Output |
|---|---|---|---|---|
| 1 | Create numeric vector | nums | c(10, 20, 30) | |
| 2 | Create character vector | chars | c("a", "b", "c") | |
| 3 | Print numeric vector | nums | c(10, 20, 30) | [1] 10 20 30 |
| 4 | Print character vector | chars | c("a", "b", "c") | [1] "a" "b" "c" |
| 5 | End of code | Execution complete |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| nums | NULL | c(10, 20, 30) | c(10, 20, 30) | c(10, 20, 30) |
| chars | NULL | NULL | c("a", "b", "c") | c("a", "b", "c") |
Numeric and character vectors in R hold numbers or text.
Use c() to create vectors: nums <- c(1,2,3), chars <- c("a","b","c").
Vectors hold one type only; mixing types converts all to character.
Print vectors with print() or just variable name.
Vectors are basic building blocks for data in R.c(1, 2, 3, 4) has only numbers, so it is a numeric vector. c("one", "two", "three") has text, c(TRUE, FALSE, TRUE) has logical values, and c(1, "two", 3) mixes numbers and text, which converts all to characters.c("apple", "banana", "cherry") has all elements as quoted text strings, so it is a character vector. c(1, 2, 3) is numeric, c(TRUE, FALSE, TRUE) is logical, and c(1, "banana", 3) mixes types causing coercion.x <- c(5, 10, 15)
y <- c("a", "b", "c")
z <- c(x, y)
print(z)
vec <- c(1, 2, "three", 4)
sum(vec)
nums <- c(2, 4, 6, 8)
chars <- c("2", "4", "6", "8")
chars and add it to nums element-wise?Pass the test and this pattern moves unknown