Code
library(reticulate)Tony Duan
January 26, 2023
##character
当检查变量懂的数据类型或者数据结构,建议使用class()而不是typeof()
##numeric
##complex
One number is a vector too.
create vector
append vector
calculate vector
[,1] [,2] [,3] [,4]
[1,] 201 205 209 213
[2,] 202 206 210 214
[3,] 203 207 211 215
[4,] 204 208 212 216
check size,it 4 by 4 matrix
check row
check column
Transpose
row append two matrix
[,1] [,2] [,3] [,4]
[1,] 201 205 209 213
[2,] 202 206 210 214
[3,] 203 207 211 215
[4,] 204 208 212 216
[5,] 1 5 9 13
[6,] 2 6 10 14
[7,] 3 7 11 15
[8,] 4 8 12 16
column append two matrix
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 201 205 209 213 1 5 9 13
[2,] 202 206 210 214 2 6 10 14
[3,] 203 207 211 215 3 7 11 15
[4,] 204 208 212 216 4 8 12 16
selects the element at the first row
select the 6th element at the matrix
selects the element at the 1,2 row
selects the element at the first column
selects the element at the first row and second column.
[1] "data.frame"
[1] "numeric"
[1] "character"
height weight names
1 180 65 Joanna
2 155 50 Charlotte
3 160 52 Helen
4 167 58 Karen
5 181 70 Amy
check size,it 5 row by 3 column data frame
check row
check column
Transpose
[,1] [,2] [,3] [,4] [,5]
height "180" "155" "160" "167" "181"
weight "65" "50" "52" "58" "70"
names "Joanna" "Charlotte" "Helen" "Karen" "Amy"
row append two dataframe
height weight names
1 180 65 Joanna
2 155 50 Charlotte
3 160 52 Helen
4 167 58 Karen
5 181 70 Amy
21 155 50 Charlotte
column append two dataframe
selects the element at the first row
selects the element at the 1,2 row
selects the element at the first and second column
selects the element at the first row and second column.
http://adv-r.had.co.nz/Data-structures.html#vectors
---
title: "R中的数据类型与数据结构"
author: "Tony Duan"
date: "2023-01-26"
categories: [R]
execute:
warning: false
error: false
format:
html:
code-fold: show
code-tools: true
---
```{r}
library(reticulate)
```
# 数据类型 datatype
##character
当检查变量懂的数据类型或者数据结构,建议使用class()而不是typeof()
```{r}
x <- "dataset"
class(x)
```
##numeric
```{r}
x <- 123
class(x)
```
##complex
```{r}
x <- 3 + 2i
class(x)
```
```{r}
x <- TRUE
class(x)
```
# 数据结构 data structures
## Vector
One number is a vector too.
```{r}
y <- 1
y
class(y)
```
```{r}
y <- c(1,2,3)
y
class(y)
```
create vector
```{r}
seq(from = 2, to = 14, by = 2)
```
```{r}
rep(x = 1.5, times = 4)
```
append vector
```{r}
x=c(1,2,3)
y=c(4,5,6)
z=c(x,y)
z
```
calculate vector
```{r}
x=c(1,2,3)
```
```{r}
sum(x)
```
```{r}
mean(x)
```
```{r}
x+10
```
## Matrix
```{r}
x <- matrix(201:216, nrow = 4)
y <- matrix(1:16, nrow = 4)
class(y)
```
```{r}
x
```
```{r}
y
```
check size,it 4 by 4 matrix
```{r}
dim(x)
```
check row
```{r}
nrow(x)
```
check column
```{r}
ncol(x)
```
### matrix calculation
Transpose
```{r}
t(y)
```
row append two matrix
```{r}
rbind(x,y)
```
column append two matrix
```{r}
cbind(x,y)
```
```{r}
x+y
```
### select matrix elements
selects the element at the first row
```{r}
y[1,]
```
select the 6th element at the matrix
```{r}
y[6]
```
selects the element at the 1,2 row
```{r}
y[1:2,]
```
selects the element at the first column
```{r}
y[,1]
```
selects the element at the first row and second column.
```{r}
y[1,2]
```
## List
```{r}
y <- list(c("black", "yellow", "orange"),
c(TRUE, TRUE, FALSE, TRUE, FALSE, FALSE),
matrix(1:6, nrow = 3))
class(y)
```
## Dataframe
```{r}
height <- c(180, 155, 160, 167, 181)
weight <- c(65, 50, 52, 58, 70)
names <- c("Joanna", "Charlotte", "Helen", "Karen", "Amy")
y <- data.frame(height = height, weight = weight, names = names)
x1=y[2,]
x2=y[,1]
class(y)
class(y$height)
class(y$names)
```
```{r}
y
```
check size,it 5 row by 3 column data frame
```{r}
dim(y)
```
check row
```{r}
nrow(y)
```
check column
```{r}
ncol(y)
```
### dataframe calculation
Transpose
```{r}
z=t(y)
z
```
row append two dataframe
```{r}
rbind(y,x1)
```
column append two dataframe
```{r}
cbind(y,x2)
```
### select dataframe elements
selects the element at the first row
```{r}
y[1,]
```
selects the element at the 1,2 row
```{r}
y[1:2,]
```
selects the element at the first and second column
```{r}
y[,1:2]
```
selects the element at the first row and second column.
```{r}
y[1,2]
```
# Reference
http://adv-r.had.co.nz/Data-structures.html#vectors