R基本的数据管理

目录

一,创建变量

二,变量重编码

三,变量的重命名

四,处理缺失值

五,日期值的使用

六,类型转换

 七,数据集的合并

八,子集的提取

一,创建变量

创建一个数据框

> myData<-data.frame(x1=c(1,2,3,4,5,6),x2=c(6,5,67,8,9,0))
> myData
  x1 x2
1  1  6
2  2  5
3  3 67
4  4  8
5  5  9
6  6  0

增加一列为两者的和

> myData$sum<-myData$x1+myData$x2
> myData
  x1 x2 sum
1  1  6   7
2  2  5   7
3  3 67  70
4  4  8  12
5  5  9  14
6  6  0   6

删除一列

> myData<-myData[,-3]
> myData
  x1 x2
1  1  6
2  2  5
3  3 67
4  4  8
5  5  9
6  6  0

二,变量重编码

创建一个数据框

manager <-c(1,2,3,4,5)
date <-c("10724/08","10/28/08","10/1/08","10/12/08","5/1/09")
age<-c(32,45,25,39,99)
q2<-c(4,5,5,3,2)
q3<-c(5,2,5,4,1)
q4<- c(5,5,5,NA,2)
q5<- c(5,5,2,NA,1)
survey <- data.frame(manager, date, age, q2, q3, q4,q5,stringsAsFactorS=FALSE)
survey
  manager     date age q2 q3 q4 q5 stringsAsFactorS
1       1 10724/08  32  4  5  5  5            FALSE
2       2 10/28/08  45  5  2  5  5            FALSE
3       3  10/1/08  25  5  5  5  2            FALSE
4       4 10/12/08  39  3  4 NA NA            FALSE
5       5   5/1/09  99  2  1  2  1            FALSE

将年龄大于99岁的值设为NA

> survey$age[survey$age==99]<-NA
> survey
  manager     date age q2 q3 q4 q5 stringsAsFactorS
1       1 10724/08  32  4  5  5  5            FALSE
2       2 10/28/08  45  5  2  5  5            FALSE
3       3  10/1/08  25  5  5  5  2            FALSE
4       4 10/12/08  39  3  4 NA NA            FALSE
5       5   5/1/09  NA  2  1  2  1            FALSE

将大于75岁的置为老年人,小于50岁的置为中青年

> survey$age[survey$age>75]<-"老年人"
> survey$age[survey$age<50]<-"中青年"
> survey
  manager     date    age q2 q3 q4 q5 stringsAsFactorS
1       1 10724/08 中青年  4  5  5  5            FALSE
2       2 10/28/08 中青年  5  2  5  5            FALSE
3       3  10/1/08 中青年  5  5  5  2            FALSE
4       4 10/12/08 中青年  3  4 NA NA            FALSE
5       5   5/1/09   <NA>  2  1  2  1            FALSE

三,变量的重命名

直接使用fix函数修改

> survey
  manager     date   年龄 q2 q3 q4 q5 stringsAsFactorS
1       1 10724/08 中青年  4  5  5  5            FALSE
2       2 10/28/08 中青年  5  2  5  5            FALSE
3       3  10/1/08 中青年  5  5  5  2            FALSE
4       4 10/12/08 中青年  3  4 NA NA            FALSE
5       5   5/1/09   <NA>  2  1  2  1            FALSE

 第二种

> names(survey)[2]<-"日期"
> survey
  manager     日期   年龄 q2 q3 q4 q5 stringsAsFactorS
1       1 10724/08 中青年  4  5  5  5            FALSE
2       2 10/28/08 中青年  5  2  5  5            FALSE
3       3  10/1/08 中青年  5  5  5  2            FALSE
4       4 10/12/08 中青年  3  4 NA NA            FALSE
5       5   5/1/09   <NA>  2  1  2  1            FALSE

四,处理缺失值

r的缺失值用NA表示,NA表示不可用的

> x<-c(1,2,3,4,5,NA)
> is.na(x)
[1] FALSE FALSE FALSE FALSE FALSE  TRUE

针对空值进行的任何比较和运算 都是空值

na.omit=TRUE 删除NA

删除NA所在的行:

> manager <-c(1,2,3,4,5)
> date <-c("10724/08","10/28/08","10/1/08","10/12/08","5/1/09")
> age<-c(32,45,25,39,99)
> q2<-c(4,5,5,3,2)
> q3<-c(5,2,5,4,1)
> q4<- c(5,5,5,NA,2)
> q5<- c(5,5,2,NA,1)
> survey <- data.frame(manager, date, age, q2, q3, q4,q5,stringsAsFactorS=FALSE)
> survey
  manager     date age q2 q3 q4 q5 stringsAsFactorS
1       1 10724/08  32  4  5  5  5            FALSE
2       2 10/28/08  45  5  2  5  5            FALSE
3       3  10/1/08  25  5  5  5  2            FALSE
4       4 10/12/08  39  3  4 NA NA            FALSE
5       5   5/1/09  99  2  1  2  1            FALSE
> data<-na.omit(survey)
> data
  manager     date age q2 q3 q4 q5 stringsAsFactorS
1       1 10724/08  32  4  5  5  5            FALSE
2       2 10/28/08  45  5  2  5  5            FALSE
3       3  10/1/08  25  5  5  5  2            FALSE
5       5   5/1/09  99  2  1  2  1            FALSE

五,日期值的使用

 将日期格式化

> mydate<-c("01/05/2020","02/06/2023")
> date<- as.Date(mydate,"%m/%d/%Y")
> date
[1] "2020-01-05" "2023-02-06"

获取当前时间


> Sys.Date()
[1] "2024-04-26"
> date()
[1] "Fri Apr 26 19:10:44 2024"

format日期

> format(date,format="%B %d %Y")
[1] "一月 05 2020" "二月 06 2023"

日期的运算

六,类型转换

 七,数据集的合并

按列名添加

z<-merge(x,y,by="2")

按行添加

z<-rbind(x,y)

八,子集的提取

截取其中几列

> q<-survey[,2:3]
> q
      date age
1 10724/08  32
2 10/28/08  45
3  10/1/08  25
4 10/12/08  39
5   5/1/09  99

去掉其中一列

> x<-survey[,-2]
> x
  manager age q2 q3 q4 q5 stringsAsFactorS
1       1  32  4  5  5  5            FALSE
2       2  45  5  2  5  5            FALSE
3       3  25  5  5  5  2            FALSE
4       4  39  3  4 NA NA            FALSE
5       5  99  2  1  2  1            FALSE

获取大于35或者小于24的 q2 q3列

> newdate<-subset(survey,age>-35 | age<24,select = c(q2,q3))
> newdate
  q2 q3
1  4  5
2  5  2
3  5  5
4  3  4
5  2  1

随机抽样的获取

> mysample<-survey[sample(5,3,replace=FALSE),]
> mysample
  manager     date age q2 q3 q4 q5 stringsAsFactorS
4       4 10/12/08  39  3  4 NA NA            FALSE
3       3  10/1/08  25  5  5  5  2            FALSE
2       2 10/28/08  45  5  2  5  5            FALSE
>

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

袁震

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值