title: "note-3"
output: html_document
date: "2023-02-07"
R Markdown
代码语言:text复制# 练习3-1
# 1.读取exercise.csv这个文件,赋值给test。
test <- read.csv("exercise.csv")
# 2.求test第一列数值的中位数
median(test$Petal.Length)
代码语言:txt复制## [1] 4.6
代码语言:text复制# 3.筛选test中,Species列的值为a或c的行
test[test$Species!="b",]
代码语言:txt复制## Petal.Length Petal.Width Species
## 1 4.6 1.5 a
## 3 4.5 1.5 a
## 5 4.0 1.3 a
## 6 4.7 1.4 a
## 7 1.3 0.2 c
## 8 1.4 0.2 c
## 11 4.9 1.5 a
## 12 1.4 0.2 c
## 13 1.5 0.2 c
## 15 1.4 0.2 c
代码语言:text复制test[test$Species=="a"|test$Species=="c",]
代码语言:txt复制## Petal.Length Petal.Width Species
## 1 4.6 1.5 a
## 3 4.5 1.5 a
## 5 4.0 1.3 a
## 6 4.7 1.4 a
## 7 1.3 0.2 c
## 8 1.4 0.2 c
## 11 4.9 1.5 a
## 12 1.4 0.2 c
## 13 1.5 0.2 c
## 15 1.4 0.2 c
代码语言:text复制test[test$Species %in% c("a","c"),]
代码语言:txt复制## Petal.Length Petal.Width Species
## 1 4.6 1.5 a
## 3 4.5 1.5 a
## 5 4.0 1.3 a
## 6 4.7 1.4 a
## 7 1.3 0.2 c
## 8 1.4 0.2 c
## 11 4.9 1.5 a
## 12 1.4 0.2 c
## 13 1.5 0.2 c
## 15 1.4 0.2 c
代码语言:text复制# 练习3-2
# 1.统计内置数据iris最后一列有哪几个取值,每个取值重复了多少次
iris[,ncol(iris)]
代码语言:txt复制## [1] setosa setosa setosa setosa setosa setosa
## [7] setosa setosa setosa setosa setosa setosa
## [13] setosa setosa setosa setosa setosa setosa
## [19] setosa setosa setosa setosa setosa setosa
## [25] setosa setosa setosa setosa setosa setosa
## [31] setosa setosa setosa setosa setosa setosa
## [37] setosa setosa setosa setosa setosa setosa
## [43] setosa setosa setosa setosa setosa setosa
## [49] setosa setosa versicolor versicolor versicolor versicolor
## [55] versicolor versicolor versicolor versicolor versicolor versicolor
## [61] versicolor versicolor versicolor versicolor versicolor versicolor
## [67] versicolor versicolor versicolor versicolor versicolor versicolor
## [73] versicolor versicolor versicolor versicolor versicolor versicolor
## [79] versicolor versicolor versicolor versicolor versicolor versicolor
## [85] versicolor versicolor versicolor versicolor versicolor versicolor
## [91] versicolor versicolor versicolor versicolor versicolor versicolor
## [97] versicolor versicolor versicolor versicolor virginica virginica
## [103] virginica virginica virginica virginica virginica virginica
## [109] virginica virginica virginica virginica virginica virginica
## [115] virginica virginica virginica virginica virginica virginica
## [121] virginica virginica virginica virginica virginica virginica
## [127] virginica virginica virginica virginica virginica virginica
## [133] virginica virginica virginica virginica virginica virginica
## [139] virginica virginica virginica virginica virginica virginica
## [145] virginica virginica virginica virginica virginica virginica
## Levels: setosa versicolor virginica
代码语言:text复制table(iris$Species)
代码语言:txt复制##
## setosa versicolor virginica
## 50 50 50
代码语言:text复制# 2.提取内置数据iris的前5行,前4列,并转换为矩阵,赋值给a。
class(iris)
代码语言:txt复制## [1] "data.frame"
代码语言:text复制iris[(1:5),(1:4)]
代码语言:txt复制## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 5.1 3.5 1.4 0.2
## 2 4.9 3.0 1.4 0.2
## 3 4.7 3.2 1.3 0.2
## 4 4.6 3.1 1.5 0.2
## 5 5.0 3.6 1.4 0.2
代码语言:text复制a <- iris[(1:5),(1:4)]
a <- as.matrix(a)
a
代码语言:txt复制## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 5.1 3.5 1.4 0.2
## 2 4.9 3.0 1.4 0.2
## 3 4.7 3.2 1.3 0.2
## 4 4.6 3.1 1.5 0.2
## 5 5.0 3.6 1.4 0.2
代码语言:text复制class(a)
代码语言:txt复制## [1] "matrix" "array"
代码语言:text复制# 3.将a的行名改为flower1,flower2...flower5。
rownames(a) <- c("flower1","flower2","flower3","flower4","flower5")
rownames(a) <- paste0("flower",1:nrow(a))
a
代码语言:txt复制## Sepal.Length Sepal.Width Petal.Length Petal.Width
## flower1 5.1 3.5 1.4 0.2
## flower2 4.9 3.0 1.4 0.2
## flower3 4.7 3.2 1.3 0.2
## flower4 4.6 3.1 1.5 0.2
## flower5 5.0 3.6 1.4 0.2
代码语言:text复制# 4.探索列表取子集l[2]和l[[2]]的区别(提示:数据结构)
class(l2)
class(l[2])
代码语言:txt复制
引自生信技能树