首页 > > > 【R语言基础】05 数据整合Ⅰ
假设有MyData数据框,其中变量为x1,x2. 现创建名为sumx和meanx的新变量,分别存储变量x1与x2的加和与均值。
#方法1
MyData$sumx <- MyData$x1 + MyData$x2
MyData$meanx <- (MyData$x1 + MyData$x2)/2
#方法2
attach(MyData)
MyData$sumx <- x1+x2
MyData$meanx <- (x1+x2)/2
detach(MyData)#方法3(建议方法)
MyData <- transform(MyData,
sumx = x1 + x2,
meanx = (x1+x2)/2)
1. 用交互式编辑器
若修改数据集x的变量名,键入函数fix(x),即可打开交互式编辑器界面。
score<-data.frame(student=c("A","B","C","D"),gender=c("M","M","F","F"),math=c(90,70,80,60),Eng=c(88,78,69,98),pl=c(66,59,NA,88))
fix(score)
score.list<-as.list(score) #将score转化为列表
fix(score.list)
(1)若数据集为矩阵或数据框
将打开“数据编辑器”,单击要修改的变量名,在弹出的“变量编辑器”修改即可:

(2)若数据集为列表
将交互式编辑器为一个记事本,只需修改“.Names”之后对应的变量名即可:

2. 用函数rename()
reshape包中的函数rename(),用来修改数据框和列表的变量名,但不能修改矩阵的变量名,基本格式为:
rename(x, c(oldname="newname",...))其中,oldname为原变量名,newname为新变量名。
library(reshape)
rename(score,c(pl="chinese"))
student gender math Eng chinese
1 A M 90 88 66
2 B M 70 78 59
3 C F 80 69 NA
4 D F 60 98 88
rename(score.list,c(pl="chinese"))
$student
[1] A B C D
Levels: A B C D
$gender
[1] M M F F
Levels: F M
$math
[1] 90 70 80 60
$Eng
[1] 88 78 69 98
$chinese
[1] 66 59 NA 88
注意:原数据集中的变量名并未被修改。
3. 用函数names()
和rename()一样可用来修改数据框和列表的变量名,不能修改矩阵的变量名;区别在于:names()会在原数据集中修改变量名。
其基本格式为: names(x)[i]<- "newname"
names(score)[5]="chinese"
score
student gender math Eng chinese
1 A M 90 88 66
2 B M 70 78 59
3 C F 80 69 NA
4 D F 60 98 88
4. 用函数colnames()和rownames()
用来修改矩阵的变量名(行名和列名),也能修改数据框的行名和列名。基本格式为:
rownames(x)[i]<- "newname"
colnames(score)[5]="Chinese"
score
student gender math Eng Chinese
1 A M 90 88 66
2 B M 70 78 59
3 C F 80 69 NA
4 D F 60 98 88
rownames(score)=letters[1:4]
score
student gender math Eng Chinese
a A M 90 88 66
b B M 70 78 59
c C F 80 69 NA
d D F 60 98 88
例如,数据集Leader中连续型变量age重新编码为分类型变量rankAge (Young、Middle Aged、Elder)。
首先,必须把99岁的年龄值重新编码为缺失值:
Leader$age[Leader$age == 99] <-NA
然后创建rankAge变量:
Leader$rankAge[Leader$age>75] <- "Elder"
Leader$rankAge[Leader$age>=50 & Leader$age <=75] <- "Middle Aged"
Leader$rankAge[Leader$age<50] <- "Young"
或者
Leader<-within(Leader, {
rankAge<-NA
rankAge[age>75] <- "Elder"
rankAge[age>=50 & age<=75]<-"Middle Aged"
rankAge[age<50] <- "Young"})
1.函数sort(),基本格式:
sort(x,decreasing=FALSE, na.last= FALSE,...)
其中,
x为排序对象(数值型或字符型);
decreasing默认为FALSE即升序,TURE为降序;
na.last默认为FALSE(NA值将被删除),若为TRUE,则将向量中的NA值放到序列末尾。
sort(score$math)
[1] 60 70 80 90
sort(score$math,decreasing = TRUE)
[1] 90 80 70 60
sort(score$Chinese,na.last = TRUE)
[1] 59 66 88 NA
2.函数rank()
返回值是该向量中对应元素的秩(排名),基本格式为:
rank(x, na.last= FALSE,ties.method=...)
其中,ties.method指定对数据集中的重复数据的秩的处理方式:
“average”——取平均值秩(默认)
“first”——位于前面的数据的秩取小,依次递增
“random”——随机定义重复秩
“max”——取最大重复秩
“min”——取最小重复秩
x<-c(3,4,2,5,5,3,8,9)
rank(x)
[1] 2.5 4.0 1.0 5.5 5.5 2.5 7.0 8.0
rank(x,ties.method = "first")
[1] 2 4 1 5 6 3 7 8
rank(x,ties.method = "random")
[1] 3 4 1 6 5 2 7 8
rank(x,ties.method = "max")
[1] 3 4 1 6 6 3 7 8
3. 函数order()
对数据进行排序,返回值是对应“排名”的元素所在向量中的位置,即最小值、次小值、...、最大值所在的位置。基本格式为:
order(x,decreasing=FALSE, na.last= FALSE,...)
不同于前两个函数,order()还可以对数据框进行排序:
data_frame[order(data_frame$v1, data_frame$v2, …),]
若v1值相同,则按v2升序排序;要将升序改为降序,在变量前添加负号,或用decreasing = TRUE即可。
order(score$math)
[1] 4 2 3 1
score[order(score$math),]
student gender math Eng chinese
4 D F 60 98 88
2 B M 70 78 59
3 C F 80 69 NA
1 A M 90 88 66
score[order(-score$math),]
student gender math Eng chinese
1 A M 90 88 66
3 C F 80 69 NA
2 B M 70 78 59
4 D F 60 98 88
4. 函数rev()
求逆序,将序列进行反转,即1,2,3变成3,2,1
注:数据框转置(行列交换),使用t(data)即可。
[1] Robert I. Kabacoff著,高涛,肖楠,陈钢 译,R语言实战. 人民邮电出版社.
[2] 李诗羽,张飞,王正林,数据分析:R语言实战. 电子工业出版社.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3167641/ ,如需转载,请注明出处,否则将追究法律责任。