R 数据框:如何创建、附加、选择和子集
什么是数据框?
A 数据框 是长度相等的向量列表。矩阵仅包含一种类型的数据,而数据框则接受不同的数据类型(数字、字符、因子等)。
如何创建数据框
我们可以在 R 通过将变量 a、b、c、d 传递到 data.frame() 函数。我们可以用 R 创建数据框并使用 name() 命名列,然后简单地指定变量的名称。
data.frame(df, stringsAsFactors = TRUE)
参数:
- df:它可以是一个矩阵,转换为数据框,也可以是一组变量的集合
- 字符串作为因子:默认将字符串转换为因子
我们可以通过组合四个相同长度的变量在 R 中为我们的第一个数据集创建一个数据框。
# Create a, b, c, d variables a <- c(10,20,30,40) b <- c('book', 'pen', 'textbook', 'pencil_case') c <- c(TRUE,FALSE,TRUE,FALSE) d <- c(2.5, 8, 10, 7) # Join the variables to create a data frame df <- data.frame(a,b,c,d) df
输出:
## a b c d ## 1 10 book TRUE 2.5 ## 2 20 pen FALSE 8.0 ## 3 30 textbook TRUE 10.0 ## 4 40 pencil_case FALSE 7.0
我们可以看到列标题与变量同名。我们可以使用函数 names() 更改 R 中的列名。查看下面的 R 创建数据框示例:
# Name the data frame names(df) <- c('ID', 'items', 'store', 'price') df
输出:
## ID items store price ## 1 10 book TRUE 2.5 ## 2 20 pen FALSE 8.0 ## 3 30 textbook TRUE 10.0 ## 4 40 pencil_case FALSE 7.0
# Print the structure str(df)
输出:
## 'data.frame': 4 obs. of 4 variables: ## $ ID : num 10 20 30 40 ## $ items: Factor w/ 4 levels "book","pen","pencil_case",..: 1 2 4 3 ## $ store: logi TRUE FALSE TRUE FALSE ## $ price: num 2.5 8 10 7
默认情况下,数据框返回字符串变量作为一个因子。
切片数据框
可以对数据框的值进行切片。我们选择要返回的行和列,并将其放入数据框名称前面的括号中。
数据框由行和列组成,df[A, B]。A 表示行,B 表示列。我们可以通过指定行和/或列来进行切片。
从图 1 中,左边部分代表 行, 右边是 列. 注意符号 : 表示 至例如,1:3 表示从 1 中选择值 至 3.
在下图中我们展示了如何访问数据框的不同选择:
- 黄色箭头选择 行 1 in 栏 2
- 绿色箭头选择 行 1到2
- 红色箭头选择 栏 1
- 蓝色箭头选择 行 1年至3年以及 列 3到4
请注意,如果我们将左侧部分留空,R 将选择 所有的行以此类推,如果我们让右边的部分空白,R 会选择 所有的列.
我们可以在控制台中运行代码:
## Select row 1 in column 2 df[1,2]
输出:
## [1] book ## Levels: book pen pencil_case textbook
## Select Rows 1 to 2 df[1:2,]
输出:
## ID items store price ## 1 10 book TRUE 2.5 ## 2 20 pen FALSE 8.0
## Select Columns 1 df[,1]
输出:
## [1] 10 20 30 40
## Select Rows 1 to 3 and columns 3 to 4 df[1:3, 3:4]
输出:
## store price ## 1 TRUE 2.5 ## 2 FALSE 8.0 ## 3 TRUE 10.0
也可以使用列名来选择列。例如,下面的代码提取了两列:ID 和 store。
# Slice with columns name df[, c('ID', 'store')]
输出:
## ID store ## 1 10 TRUE ## 2 20 FALSE ## 3 30 TRUE ## 4 40 FALSE
向数据框添加一列
您还可以将列附加到数据框。您需要使用符号 $ 附加数据框 R 变量,并将列添加到 R 中的数据框。
# Create a new vector quantity <- c(10, 35, 40, 5) # Add `quantity` to the `df` data frame df$quantity <- quantity df
输出:
## ID items store price quantity ## 1 10 book TRUE 2.5 10 ## 2 20 pen FALSE 8.0 35 ## 3 30 textbook TRUE 10.0 40 ## 4 40 pencil_case FALSE 7.0 5
注意:向量中的元素数量必须等于数据框中的元素数量。执行以下语句将列添加到数据框 R
quantity <- c(10, 35, 40) # Add `quantity` to the `df` data frame df$quantity <- quantity
给出错误:
Error in ` lt;-.data.frame`(`*tmp*`, quantity, value = c(10, 35, 40)) replacement has 3 rows, data has 4
选择数据框的一列
有时,我们需要存储数据框的一列以供将来使用或对列执行操作。我们可以使用 $ 符号从数据框中选择列。
# Select the column ID df$ID
输出:
## [1] 1 2 3 4
对数据框进行子集化
在上一节中,我们无条件地选择了整列。可以 子集 根据某个条件是否真实。
我们使用 subset() 函数。
subset(x, condition) arguments: - x: data frame used to perform the subset - condition: define the conditional statement
我们只想返回价格高于 10 的商品,我们可以这样做:
# Select price above 5 subset(df, subset = price > 5)
输出:
ID items store price 2 20 pen FALSE 8 3 30 textbook TRUE 10 4 40 pencil_case FALSE 7