R Experiment 1--Introduction to R Language-Matrix Algebra, Random Variables, Simple Graphing

An experimental goal:

1. Review the basic operations of R language and be familiar with the console menu and its functions;
2. Master the creation of data sets;
3. Master basic matrix calculation methods and basic random variable digital characteristic calculation methods;
4. Understand multivariate data sets and Visualization.

2. Experimental requirements:

1. Familiar with the menu bar, working path settings, script file creation, data package loading, and use of help documents; 2. Creation of vectors,
matrices, data frames (factors), and lists;
3. External data import and analysis cases (1 set of cases).

3. Experimental operation

1. Creation of vectors

1. Experimental code

x1<- c(1,2,5,3,6,7,8)   #数值型向量
x2<- c(10,11,12,13,14,15,16)  
#矩阵的创建
#用rbin()按行合并

rbind(x1,x2)
#用cbind()按列合并
cbind(x1,x2)



#生成数据框
data.frame(x1,x2)



#尝试做其他的数据框(可忽略)
patientID<- c(1, 2, 3, 4)
age <- c(1, 4, 2, 5)
diabetes <- c("Type1", "Type2", "Type1", "Type1")
status <- c("Poor", "Improved", "Excellent", "Poor")
data.frame(patientID, age, diabetes, status)

2. Experimental results:

Please add image description

2. To create a list, use the function list() to create a list.

1. Code implementation

g <- "My List"
h <- c(5, 6, 8, 9)
j <- matrix(1:10, nrow = 5)
k <- c("one", "two", "three")
mylist <- list(title = g, ages = h, j, k)
mylist

2. Result

Please add image description

3. External data import and analysis cases (1 set of cases)

1. Measure the height (x1) and weight (x2) data of 12 middle school students, as shown in the table.
Please add image description
1) Create a vector (one-dimensional array)

x1<- c(171, 175, 159, 155, 152, 158, 154, 164, 168, 166, 159, 164)
x2<- c(57, 64, 41, 38, 35, 44, 41, 51, 57, 49, 47, 46)
length(x1)#向量长度
mode(x1)#向量类型

2) Create a matrix (two-dimensional data)

#矩阵
rbind(x1,x2)
cbind(x1,x2)

3) Generate data frame

data.frame(x1,x2)

4) Histogram

hist(x1) 
hist(x2)

5) Scatter plot

plot(x1,x2)
df <- data.frame(
  Height = c(171, 175, 159, 155, 152, 158, 154, 164, 168, 166, 159, 164),
  Weight = c(57, 64, 41, 38, 35, 44, 41, 51, 57, 49, 47, 46)
)
attach(df)
qqnorm(Height);qqline(Height)
qqnorm(Weight);qqline(Weight)

6) Scatter plot matrix

df <- data.frame(
  Height = c(171, 175, 159, 155, 152, 158, 154, 164, 168, 166, 159, 164),
  Weight = c(57, 64, 41, 38, 35, 44, 41, 51, 57, 49, 47, 46)
)
pairs(df)

df <- data.frame(
  Age = c(13, 15, 13, 14, 14, 15, 13, 12, 13, 14, 14, 13),
  Height = c(171, 175, 159, 155, 152, 158, 154, 164, 168, 166, 159, 164),
  Weight = c(57, 64, 41, 38, 35, 44, 41, 51, 57, 49, 47, 46)
)
pairs(df)

2. Experimental results

Please add image description

4. Thinking and practice questions

Insert image description here
1) First question code

#第一题
# 导入数据
x1 <- c(9,2,6,5,8)
x1
x2 <- c(12,8,6,4,10)
x2
x3 <- c(3,4,0,2,1)
x3
# 样本均值向量
mean(x1)
mean(x2)
mean(x3)

#转为矩阵
df <- data.frame(x1,x2,x3)
# 样本方差
var(df)
# 样本协方差
cov(df)
# 样本协方差矩阵
matrixNoHeader <- cbind(x1,x2,x3)
var(matrixNoHeader)
# 样本相关系数,对角线上的数值即为相关系数
cor(df)
# 样本相关矩阵
cor(df)

2) Results

Please add image description
3) Question 2

###第2题###
#散点图
plot(x1,x2)
plot(x2,x3)
plot(x1,x3)

#散布图矩阵
pairs(df)

4) Partial screenshots
Please add image description
Please add image description

5. View and call the mtcars and iris data sets in R, understand the basic information of the data, and choose at least 2 drawing methods for graphic display based on the knowledge you have learned.

1. Experimental code

#iris和mtcars数据集
dim(iris)
names(iris)
str(iris)
attributes(iris)
quantile(iris$Sepal.Length)
quantile(iris$Sepal.Length, c(.1, .3, .65))

#使用函数var查看Sepal.Length的方差,使用hist绘制分布直方图
var(iris$Sepal.Length)
hist(iris$Sepal.Length)

# 密度图
plot(density(iris$Sepal.Length))

## 条形图
barplot(table(iris$Species))

#使用函数boxplot绘制盒图,以展示数据分布的中位数、
boxplot(Sepal.Length~Species, data=iris)

  #mtcars数据集
attach(mtcars)
mtcars
pie(apply(mtcars,2,mean)) #饼图
plot(wt, mpg) #散点图

2. Experimental results (partial screenshots)

Please add image description

Please add image description

Please add image description

Guess you like

Origin blog.csdn.net/qq_62127918/article/details/130461896