R中的列表和数据框有什么区别

Python019

R中的列表和数据框有什么区别,第1张

R语言中列表(list):

向量的一种一般形式,并不需要保证其中的元素都是相同的类型,而且其中的元素经常是向量和列表本身。

数据框(data frame):

Data Frame一般被翻译为数据框,感觉就像是R中的表,由行和列组成,与列表不同的是,每个列可以是不同的数据类型,而列表是必须相同的。

Data Frame每一列有列名,每一行也可以指定行名。如果不指定行名,那么就是从1开始自增的Sequence来标识每一行。

初始化

使用data.frame函数就可以初始化一个Data Frame。比如我们要初始化一个student的Data Frame其中包含ID和Name还有Gender以及Birthdate,那么代码为:

student<-data.frame(ID=c(11,12,13),Name=c("Devin","Edward","Wenli"),Gender=c("M","M","F"),Birthdate=c("1984-12-29","1983-5-6","1986-8-8”))

另外也可以使用read.table() read.csv()读取一个文本文件,返回的也是一个Data Frame对象。读取数据库也是返回Data Frame对象。

查看student的内容为:

ID Name Gender Birthdate

1 11 Devin M 1984-12-29

2 12 Edward M 1983-5-6

3 13 Wenli F 1986-8-8

这里只指定了列名为ID,Name,Gender和Birthdate,使用names函数可以查看列名,如果要查看行名,需要用到row.names函数。这里我们希望将ID作为行名,那么可以这样写:

row.names(student)<-student$ID

更简单的办法是在初始化date.frame的时候,有参数row.names可以设置行名的向量。

访问元素

与Matrix一样,使用[行Index,列Index]的格式可以访问具体的元素。

比如访问第一行:

student[1,]

访问第二列:

student[,2]

使用列的Index或者列名可以选取要访问的哪些列。比如要ID和Name,那么代码为:

idname<-student[1:2]

或者是

idname<-student[c("ID","Name”)]

如果是只访问某一列,返回的是Vector类型的,那么可以使用[[或者$来访问。比如我们要所有student的Name,代码为:

name<-student[[2]] 或者name<-student[[“Name”]] 或者name<-student$Name

使用attach和detach函数可以使得访问列时不需要总是跟着变量名在前面。

比如要打印所有Name,那么可以写成:

attach(student)

print(Name)

detach(student)

还可以换一种简洁一点的写法就是用with函数:

with(student,{

n<-Name

print(n)

})

这里的n作用域只在大括号内,如果想在with函数中对全局的变量进行赋值,那么需要使用<<-这样一个运算符。

修改列数据类型

接下来我们查看该对象每列的类型,使用str(student)可以得到如下结果:

'data.frame':3 obs. of 4 variables:

$ ID : num 1 2 3

$ Name : Factor w/ 3 levels "Devin","Edward",..: 1 2 3

$ Gender : Factor w/ 2 levels "F","M": 2 2 1

$ Birthdate: Factor w/ 3 levels "1983-5-6","1984-12-29",..: 2 1 3

默认情况下,字符串向量都会被自动识别成Factor,也就是说,ID是数字类型,其他的3个列都被定义为Factor类型了。显然这里Name应该是字符串类型,Birthdate应该是Date类型,我们需要对列的数据类型进行更改:

student$Name<-as.character(student$Name)

student$Birthdate<-as.Date(student$Birthdate)

下面我们再运行str(student)看看修改后的结果:

'data.frame':3 obs. of 4 variables:

$ ID : num 11 12 13

$ Name : chr "Devin" "Edward" "Wenli"

$ Gender : Factor w/ 2 levels "F","M": 2 2 1

$ Birthdate: Date, format: "1984-12-29" "1983-05-06" "1986-08-08”

添加新列

对于以及存在的student对象,我们希望增加Age列,该列是根据Birthdate算出来的。首先需要知道怎么算年龄。我们可以使用日期函数Sys.Date()获得当前的日期,然后使用format函数获得年份,然后用两个年份相减就是年龄。好像R并没有提供几个能用的日期函数,我们只能使用format函数取出年份部分,然后转换为int类型相减。

student$Age<-as.integer(format(Sys.Date(),"%Y"))-as.integer(format(student$Birthdate,"%Y”))

这样写似乎太长了,我们可以用within函数,这个函数和之前提到过的with函数类似,可以省略变量名,不同的地方是within函数可以在其中修改变量,也就是我们这里增加Age列:

student<-within(student,{

Age<-as.integer(format(Sys.Date(),"%Y"))-as.integer(format(Birthdate,"%Y"))

})

查询/子集

查询一个Date Frame,返回一个满足条件的子集,这相当于数据库中的表查询,是非常常见的操作。使用行和列的Index来获取子集是最简单的方法,前面已经提到过。如果我们使用布尔向量,配合which函数,可以实现对行的过滤。比如我们要查询所有Gender为F的数据,那么我们首先对student$Gender==“F”,得到一个布尔向量:FALSE FALSE TRUE,然后使用which函数可以将布尔向量中TRUE的Index返回,所以我们的完整查询语句就是:

student[which(student$Gender=="F"),]

注意这里列Index并没有输入,如果我们只想知道所有女生的年龄,那么可以改为:

student[which(student$Gender=="F"),"Age”]

这样的查询写法还是复杂了点,可以直接使用subset函数,那么查询会简单些,比如我们把查询条件改为年龄<30的女性,查姓名和年龄,那么查询语句为:

subset(student,Gender=="F" &Age<30 ,select=c("Name","Age"))

R语言基本数据分析

本文基于R语言进行基本数据统计分析,包括基本作图,线性拟合,逻辑回归,bootstrap采样和Anova方差分析的实现及应用。

不多说,直接上代码,代码中有注释。

1. 基本作图(盒图,qq图)

#basic plot

boxplot(x)

qqplot(x,y)

2. 线性拟合

#linear regression

n = 10

x1 = rnorm(n)#variable 1

x2 = rnorm(n)#variable 2

y = rnorm(n)*3

mod = lm(y~x1+x2)

model.matrix(mod) #erect the matrix of mod

plot(mod) #plot residual and fitted of the solution, Q-Q plot and cook distance

summary(mod) #get the statistic information of the model

hatvalues(mod) #very important, for abnormal sample detection

3. 逻辑回归

#logistic regression

x <- c(0, 1, 2, 3, 4, 5)

y <- c(0, 9, 21, 47, 60, 63) # the number of successes

n <- 70 #the number of trails

z <- n - y #the number of failures

b <- cbind(y, z) # column bind

fitx <- glm(b~x,family = binomial) # a particular type of generalized linear model

print(fitx)

plot(x,y,xlim=c(0,5),ylim=c(0,65)) #plot the points (x,y)

beta0 <- fitx$coef[1]

beta1 <- fitx$coef[2]

fn <- function(x) n*exp(beta0+beta1*x)/(1+exp(beta0+beta1*x))

par(new=T)

curve(fn,0,5,ylim=c(0,60)) # plot the logistic regression curve

3. Bootstrap采样

# bootstrap

# Application: 随机采样,获取最大eigenvalue占所有eigenvalue和之比,并画图显示distribution

dat = matrix(rnorm(100*5),100,5)

no.samples = 200 #sample 200 times

# theta = matrix(rep(0,no.samples*5),no.samples,5)

theta =rep(0,no.samples*5)

for (i in 1:no.samples)

{

j = sample(1:100,100,replace = TRUE)#get 100 samples each time

datrnd = dat[j,]#select one row each time

lambda = princomp(datrnd)$sdev^2#get eigenvalues

# theta[i,] = lambda

theta[i] = lambda[1]/sum(lambda)#plot the ratio of the biggest eigenvalue

}

# hist(theta[1,]) #plot the histogram of the first(biggest) eigenvalue

hist(theta)#plot the percentage distribution of the biggest eigenvalue

sd(theta)#standard deviation of theta

#上面注释掉的语句,可以全部去掉注释并将其下一条语句注释掉,完成画最大eigenvalue分布的功能

4. ANOVA方差分析

#Application:判断一个自变量是否有影响 (假设我们喂3种维他命给3头猪,想看喂维他命有没有用)

#

y = rnorm(9)#weight gain by pig(Yij, i is the treatment, j is the pig_id), 一般由用户自行输入

#y = matrix(c(1,10,1,2,10,2,1,9,1),9,1)

Treatment <- factor(c(1,2,3,1,2,3,1,2,3)) #each {1,2,3} is a group

mod = lm(y~Treatment) #linear regression

print(anova(mod))

#解释:Df(degree of freedom)

#Sum Sq: deviance (within groups, and residuals) 总偏差和

# Mean Sq: variance (within groups, and residuals) 平均方差和

# compare the contribution given by Treatment and Residual

#F value: Mean Sq(Treatment)/Mean Sq(Residuals)

#Pr(>F): p-value. 根据p-value决定是否接受Hypothesis H0:多个样本总体均数相等(检验水准为0.05)

qqnorm(mod$residual) #plot the residual approximated by mod

#如果qqnorm of residual像一条直线,说明residual符合正态分布,也就是说Treatment带来的contribution很小,也就是说Treatment无法带来收益(多喂维他命少喂维他命没区别)

如下面两图分别是

(左)用 y = matrix(c(1,10,1,2,10,2,1,9,1),9,1)和

(右)y = rnorm(9)

的结果。可见如果给定猪吃维他命2后体重特别突出的数据结果后,qq图种residual不在是一条直线,换句话说residual不再符合正态分布,i.e., 维他命对猪的体重有影响。

重编码涉及根据同一个变量和/或其他变量的现有值创建新值的过程,如将符合某个条件的值重新赋值等,这里主要介绍两种常见的方法:

#第一种方法

per <- data.frame(name = c("张三","李四","王五","赵六"),

age = c(23,45,34,1000))

per

per$age[per$age == 1000] <- NA #设置缺失值

per$age1[per$age <30] = "young" #生成新变量

per$age1[per$age >= 30 &per$age<50] <- "middle age"

per

#第二种方法

per <- data.frame(name = c("张三","李四","王五","赵六"),

age = c(23,45,34,1000))

per <- within(per,{

age1 <- NA

age1[age <30] <- "young"

age1[age>=30 &age<50] <- "middle age"

})

per