A data frame is used for storing data tables. It is a list of vectors of equal length. For example, the following variable df is a data frame containing three vectors n, s, b.
> n = c(2, 3, 5) > s = c("aa", "bb", "cc") > b = c(TRUE, FALSE, TRUE) > df = data.frame(n, s, b) # df is a data frame
Build-in Data Frame
We use built-in data frames in R for our tutorials. For example, here is a built-in
data frame in R, called mtcars.
The top line of the table, called the header, contains the column names. Each
horizontal line afterward denotes a data row, which begins with the name of the
row, and then followed by the actual data. Each data member of a row is called a
cell.
To retrieve data in a cell, we would enter its row and column coordinates in the
single square bracket "[]" operator. The two coordinates are separated by a comma.
In other words, the coordinates begins with row position, then followed by a comma,
and ends with the column position. The order is important.
Here is the cell value from the first row, second column of mtcars.
> mtcars[1, 2] [1] 6
Moreover, we can use the row and column names instead of the numeric
coordinates.
> mtcars["Mazda RX4", "cyl"] [1] 6
Lastly, the number of data rows in the data frame is given by the nrow function.
> nrow(mtcars) # number of data rows [1] 32
And the number of columns of a data frame is given by the ncol function.
> ncol(mtcars) # number of columns [1] 11
Further details of the mtcars data set is available in the R documentation.
> help(mtcars)
Preview
Instead of printing out the entire data frame, it is often desirable to preview it with
the head function beforehand.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2011-05-15 科研学术:如何快速的找到免费全文文献
2011-05-15 资料查阅技巧分享
2010-05-15 Microsoft Report Viewer Redistributable 2010
2009-05-15 BlogEngine.NET 1.5 出现乱码错误