#update R#R.version#Update Rstudio #rstudioapi::versionInfo()#check R package location#.libPaths()#Update R package# list all packages where an update is available#old.packages()# update all available packages#update.packages()
2. 数据类型 datatype
##character
当检查变量懂的数据类型或者数据结构,建议使用class()而不是typeof()
Code
x <-"dataset"class(x)
[1] "character"
##numeric
Code
x <-123class(x)
[1] "numeric"
##complex
Code
x <-3+ 2iclass(x)
[1] "complex"
Code
x <-TRUEclass(x)
[1] "logical"
数据结构 data structures
Vector
One number is a vector too.
Code
y <-1y
[1] 1
Code
class(y)
[1] "numeric"
Code
y <-c(1,2,3)y
[1] 1 2 3
Code
class(y)
[1] "numeric"
create vector
Code
seq(from =2, to =14, by =2)
[1] 2 4 6 8 10 12 14
Code
rep(x =1.5, times =4)
[1] 1.5 1.5 1.5 1.5
append vector
Code
x=c(1,2,3)y=c(4,5,6)z=c(x,y)z
[1] 1 2 3 4 5 6
calculate vector
Code
x=c(1,2,3)
Code
sum(x)
[1] 6
Code
mean(x)
[1] 2
Code
x+10
[1] 11 12 13
Matrix
Code
x <-matrix(201:216, nrow =4)y <-matrix(1:16, nrow =4)class(y)
---title: "R中的数据类型与数据结构"author: "Tony Duan"date: "2023-01-26"categories: [R]execute: warning: false error: falseformat: html: code-fold: show code-tools: true---# 1. 查看版本```{r}#| eval: false#update R#R.version#Update Rstudio #rstudioapi::versionInfo()#check R package location#.libPaths()#Update R package# list all packages where an update is available#old.packages()# update all available packages#update.packages()```# 2. 数据类型 datatype##character当检查变量懂的数据类型或者数据结构,建议使用class()而不是typeof()```{r}x <-"dataset"class(x)```##numeric```{r}x <-123class(x)```##complex```{r}x <-3+ 2iclass(x)``````{r}x <-TRUEclass(x)```# 数据结构 data structures## VectorOne number is a vector too.```{r}y <-1yclass(y)``````{r}y <-c(1,2,3)yclass(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 elementsselects 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 elementsselects 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] ```# Referencehttp://adv-r.had.co.nz/Data-structures.html#vectors