常见的数据框提取
以iris数据为例
iris
head(iris)
#————————————————————————————————————————————————————————————————————————
#首先提取行和列
#以提Sepal.Length这一列
x <- iris[,1] #这是数值型
x <- as.data.frame(iris[,1]) #这样提出来是数据库的格式
#提Petal.Length Petal.Width,Petal.Width这两列,这样提出来是数据库的格式
y <- iris[,c(2:4)]
#提Petal.Length,Species这两列
yy <- iris[,c(2,5)]
#提第一行
iris[1,]
#提5-10行,这样提出来是数据库的格式
xx <- iris[c(5:10),]
#假如想提取某一行某一列中的一个
head(iris)
#第二行第三列
iris[2,3]
#第三行第五列
iris[3,5]
#__________________________________________________________________
#选择性的提取---筛选
#比如提取Sepal.Width<3的数据
#第一种方式---一定要注意这个逗号
iris[which(iris$Sepal.Width<3),]
#比如提取Sepal.Width==3的数据,切记R语言里面等于号为==
iris[which(iris$Sepal.Width==3),]
#如果我们想提取Sepal.Width==3或Species为versicolor的数据时用|连接
#记住这里Species这一列为字符型,一定要带引号
iris[which((iris$Sepal.Width==3)|(iris$Species == "versicolor")),]