type
status
date
slug
summary
tags
category
icon
password













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 |
- Author:Entropyobserver
- URL:https://tangly1024.com/article/1c6d698f-3512-81fa-8a79-c48b9a41e508
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!