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.
> 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.
mpg cyl disp hp drat wt ...
Mazda RX4 21.0 6 160 110 3.90 2.62 ...
Mazda RX4 Wag 21.0 6 160 110 3.90 2.88 ...
Datsun 710 22.8 4 108 93 3.85 2.32 ...
............
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.
[1] 6
Moreover, we can use the row and column names instead of the numeric
coordinates.
[1] 6
Lastly, the number of data rows in the data frame is given by the nrow function.
[1] 32
And the number of columns of a data frame is given by the ncol function.
[1] 11
Further details of the mtcars data set is available in the R documentation.
Preview
Instead of printing out the entire data frame, it is often desirable to preview it with
the head function beforehand.
mpg cyl disp hp drat wt ...
Mazda RX4 21.0 6 160 110 3.90 2.62 ...
............
访问元素
student[1,]
student[,2]
idname<-student[1:2]
idname<-student[c("ID","Name”)]
name<-student[[2]] 或者name<-student[[“Name”]] 或者name<-student$Name
attach(student)
print(Name)
detach(student)
with(student,{
n<-Name
print(n)
})
修改列数据类型
student$Name<-as.character(student$Name)
student$Birthdate<-as.Date(student$Birthdate)
添加新列
student$Age<-as.integer(format(Sys.Date(),"%Y"))-as.integer(format(student$Birthdate,"%Y”))
student<-within(student,{
Age<-as.integer(format(Sys.Date(),"%Y"))-as.integer(format(Birthdate,"%Y"))
})
查询/子集
Frame,返回一个满足条件的子集,这相当于数据库中的表查询,是非常常见的操作。使用行和列的Index来获取子集是最简单的方法,前面已经提到过。如果我们使用布尔向量,配合which函数,可以实现对行的过滤。比如我们要查询所有Gender为F的数据,那么我们首先对student$Gender==“F”,得到一个布尔向量:FALSE
FALSE TRUE,然后使用which函数可以将布尔向量中TRUE的Index返回,所以我们的完整查询语句就是:
student[which(student$Gender=="F"),]
student[which(student$Gender=="F"),"Age”]
subset(student,Gender=="F" & Age<30 ,select=c("Name","Age"))
x.sub1 <- subset(x.df, y > 2 & V1 > 0.6)
newdata <- subset(mydata, age >= 20 | age < 10, select=c(ID, Weight))
newdata <- subset(mydata, sex=="m" & age > 25, select=weight:income)
x.sub2 <- subset(x.df, y > 2 & V2 > 0.4, select = c(V1, V4))
x.sub3 <- subset(x.df, y > 3, select = V2:V5)
x.sub4 <- x.df[x.df$y == 1, ]
x.sub5 <- x.df[x.df$y %in% c(1, 4), ]
library(sqldf)
result<-sqldf("select Name,Age from student where Gender='F' and Age<30")
连接/合并
score<-data.frame(SID=c(11,11,12,12,13),Course=c("Math","English","Math","Chinese","Math"),Score=c(90,80,80,95,96))
result<-merge(student,score,by.x="ID",by.y="SID")
student2<-data.frame(ID=c(21,22),Name=c("Yan","Peng"),Gender=c("F","M"),Birthdate=c("1982-2-9","1983-1-16"),Age=c(32,31))
rbind(student,student2)