Lazy loaded image
Technology
Lazy loaded imageR BASIC
Words 15Read Time 1 min
Jul 2, 2021
Jun 4, 2025
type
status
date
slug
summary
tags
category
icon
password
notion image
notion image
notion image
notion image
notion image
notion image
notion image
notion image
notion image
notion image
notion image
notion image
notion image

1.R Data Structures Summary Table

Category
Topic / Method
Example Code
Output / Explanation
Objects
Create objects
x <- 8
x is a numeric object: 8
hw <- "Hello World!"
hw is a character string: "Hello World!"
Character vs numeric
"7"
A character string, not a number
7
A numeric value, usable in calculations
Vectors
Create a numeric vector
vec1 <- c(2, 3, 5, 7)
[1] 2 3 5 7
Combine vectors
vec2 <- c(11, 13, 17, 19)c(vec1, vec2)
[1] 2 3 5 7 11 13 17 19
Element-wise operations
vec1 + vec2
[1] 13 16 22 26
vec1 + 1
[1] 3 4 6 8
Character vector
text.vec <- c("A", "B", "C")
class(text.vec)"character"length(text.vec)3
Mixed vector
mix.vec <- c("Hello", 3, 5)
[1] "Hello" "3" "5" (coerced to character)
Indexing
vec1[2]
[1] 3
vec1[c(1,4)]
[1] 2 7
vec1[-2]
[1] 2 5 7
Replace element
x <- 1:10; x[10] <- 99
x becomes [1] 1 2 3 4 5 6 7 8 9 99
Logical condition
x[x > 5]
[1] 6 7 8 9 99
Logical + another vector
y <- c(1,2,1,2,1,3,1,3,1,4)x[y == 1]
[1] 1 3 5 7 9 (select where y == 1)
matrix
Create matrix
x <- 1:10y <- c(1,2,1,2,1,3,1,3,1,4)mat1 <- cbind(x, y)
Creates a 10×2 numeric matrix mat1 with columns x and y: x y [1,] 1 1 [2,] 2 2 [3,] 3 1 ... [10,] 10 4
Access element
mat1[2, 1]
Gets element in row 2, column 1 → 2
Access column
mat1[, 2]
Gets entire second column (y): 1 2 1 2 1 3 1 3 1 4
Access rows
mat1[c(2, 7), ]
Gets rows 2 and 7: 2 2 7 1
Character matrix
cbind(c(1, 2, 3), c("A", "B", "C"))
Mixed types force all to character: [,1] [,2] [1,] "1" "A" [2,] "2" "B" [3,] "3" "C"
Data Frames
Create data frame
x <- 1:10 y <- c(1,2,1,2,1,3,1,3,1,4) df1 <-data.frame(x, y)
Creates a data frame with two columns x and y:head(df1): x y 1 1 1 2 2 2 3 3 1 ...
Named columns
df2 <- data.frame(var1 = x, var2 = y)
Creates data frame with columns named var1 and var2: head(df2): var1 var2 1 1 1 2 2 2 ...
Access elements
df1[, 2]
Accesses second column (y): [1] 1 2 1 2 1 3 1 3 1 4
Access elements
df1$y
Same as above: returns vector of y values
Access single value
df1$x[3]
Third element of column x → 3
Add new column
df1$z <- seq(2, 20, by = 2)
Adds new column z with even numbers: df1$z:2 4 6 8 10 12 14 16 18 20
Add constant column
df1$u <- 1
Adds new column u with all values equal to 1
Lists
Create a list
mylist <- list(vec1, mat1, df1) Assume: vec1 <- c(2, 3, 5, 7) mat1 <- cbind(x = 1:3, y = c(4, 5, 6)) df1 <- data.frame(a = 1:2, b = c("A", "B"))
mylist[[1]] = vec1 → 2 3 5 7 mylist[[2]] = mat1 →  [,1] [,2]  [1,] 1 4  [2,] 2 5  [3,] 3 6 mylist[[3]] = df1 → 2-row data frame
Mixed elements
person <- list(name = "Alice", scores = c(90, 85))
A named list. Access with: person$name → "Alice" person$scores → 90 85
Length flexibility
lengths(mylist)
Returns lengths of each element:e.g. 4 6 2 vec1 has 4 items, mat1 has 6 (3×2), df1 has 2 rows
 

Steps and Parameters for Creating a Barplot with barplot() in R

Step
Code Example / Parameter
Description
Notes
Prepare Data
med_count <- table(Doctor$medicine)
Create a frequency table for the variable
table() generates counts of each category
Call barplot()
barplot(med_count)
Draw the barplot
Takes a frequency table or numeric vector
Add Title
main = "Number of Medicines Used in the Past 2 Days"
Title of the plot
Displayed at the top of the plot window
Set X-axis Label
xlab = "Number of Medicines"
Label for the X-axis
Describes categories on the x-axis
Set Y-axis Label
ylab = "Number of Individuals"
Label for the Y-axis
Describes the height of the bars
Set Color
col = "skyblue"
Fill color of the bars
Can be color names or color codes
Set Border Color
border = "black"
Color of the bar borders
Default is black; can be changed or removed
Other Parameters
las, beside, space, ylim, etc.
Axis label rotation, grouped bars, spacing, axis limits
Advanced customization options
 
上一篇
A/B Testing in E-Commerce
下一篇
SQL 50 FROM LEECODE