Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
# Creating vectors nums <- c(4, 8, 15, 16, 23, 42) words <- c("apple", "banana", "cherry") flags <- c(TRUE, FALSE, TRUE, TRUE) # Sequences and repetitions seq1 <- 1:10 # 1 2 3 ... 10 seq2 <- seq(0, 1, by = 0.25) # 0.00 0.25 0.50 0.75 1.00 seq3 <- seq(2, 20, length.out = 5) # 5 evenly spaced values reps <- rep(c(1, 2), times = 3) # 1 2 1 2 1 2 reps2 <- rep(c(1, 2), each = 3) # 1 1 1 2 2 2 # Element-wise arithmetic a <- c(1, 2, 3, 4) b <- c(10, 20, 30, 40) a + b # 11 22 33 44 b - a # 9 18 27 36 a * b # 10 40 90 160 b / a # 10 10 10 10 a ^ 2 # 1 4 9 16 sqrt(b) # 3.16 4.47 5.48 6.32 # Logical operations (element-wise) a > 2 # FALSE FALSE TRUE TRUE a == 2 # FALSE TRUE FALSE FALSE # Subsetting nums[1] # 4 (1-based) nums[c(1, 3)] # 4 15 nums[-2] # drop the 2nd element: 4 15 16 23 42 nums[nums > 15] # 16 23 42 — logical subsetting # Named vectors ages <- c(Alice = 30, Bob = 25, Carol = 35) ages["Bob"] # 25 names(ages) # "Alice" "Bob" "Carol"
Result
Open