R语言基础 | 向量及数据框的生成、拼接、引用

向量的创建、拼接、转frame

# 创建向量
a = c("qiaofen","ouyangfeng","wangyuyan","zhagnwuji","renyingying")
b = c(-1/0,1/0,100,100,100)
# 向量拼接
rab = rbind(a,b)
cab = cbind(a,b)
# 转换成数据框
frab = data.frame(rab)
fcab = data.frame(cab)

生成数据框

# 通过data.frame函数生成数据框
c = data.frame(x = 1, y = 1:3)

数据框的引用(使用iris数据集示例)

 

# 选择 Species列 =="setosa"的 的 前1-2列数据
v = iris[which(iris$Species == "setosa"),1:2]

# 选择 iris$Sepal.Width>3.7&iris$Species=="setosa" 的所有列(特征)
w = iris[which(iris$Sepal.Width>3.7&iris$Species=="setosa"),]

# Sepal.Width 特征中最大的值 所对应的样本的信息
x= iris[which.max(iris$Sepal.Width),]

# 得到 Petal.Length 列最小的那个值
y = iris[which.min(iris$Petal.Length),which(names(iris)=="Petal.Length")]

# Species 这个特征的枚举项都有谁
z = levels(iris$Species)

 

原文地址:https://www.cnblogs.com/ykit/p/12494491.html