R基本的数据管理

news/2024/9/22 15:30:56/

一,创建变量

创建一个数据框

> myData<-data.frame(x1=c(1,2,3,4,5,6),x2=c(6,5,67,8,9,0))
> myDatax1 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
> myDatax1 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]
> myDatax1 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)
surveymanager     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
> surveymanager     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]<-"中青年"
> surveymanager     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函数修改

> surveymanager     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]<-"日期"
> surveymanager     日期   年龄 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)
> surveymanager     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)
> datamanager     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]
> qdate 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]
> xmanager 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))
> newdateq2 q3
1  4  5
2  5  2
3  5  5
4  3  4
5  2  1

随机抽样的获取

> mysample<-survey[sample(5,3,replace=FALSE),]
> mysamplemanager     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
>


http://www.ppmy.cn/news/1446301.html

相关文章

Kubernetes(K8S) — 生产环境

生产环境 生产质量的 Kubernetes 集群需要规划和准备。 如果你的 Kubernetes 集群是用来运行关键负载的&#xff0c;该集群必须被配置为弹性的&#xff08;Resilient&#xff09;。 生产环境考量 需要考虑的因数 可用性&#xff1a;一个单机的 Kubernetes 学习环境 具有单点…

给vue配置路径别名@

使用vite构建的项目 在vite.config.js中进行别名的配置 import { defineConfig } from vite import vue from vitejs/plugin-vueexport default defineConfig({plugins: [vue()],resolve: {alias: {: /src}} }) //这样&#xff0c;你就可以使用作为别名来引用/src目录下的文件…

UE5 GAS开发P35,36,37,38,39 将药水修改为AbilitySystem效果

这几节课都是将药水修改成更方便使用的AbilitySystem效果的Actor,分别为增加血量,增加蓝量,暂时获得最大生命值上限 AuraEffectActor.h // Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #…

1013: 【C1】【循环】【for】求整数的和与均值

题目描述 读入n&#xff08;1 < n < 10000&#xff09;个整数&#xff0c;求它们的和与均值 输入 输入第一行是一个整数n&#xff0c;表示有n个整数。 第2~n1行每行包含1个整数。每个整数的绝对值均不超过10000。 输出 输出一行&#xff0c;先输出和&#xff0c;再…

数据重排——Rearrange

示例&#xff1a;Rearrange(b c (h p1) (w p2) -> b (c p1 p2) h w, p12, p22) 数据重排&#xff08;rearrange&#xff09;通常用于深度学习框架中调整多维数据的维度顺序。这种操作在处理图像数据、执行矩阵乘法或构建如卷积神经网络&#xff08;CNN&#xff09;等架构时非…

C#中如何定义带参数的EventHandler?

简述 事件调用的所有方法都需要两个参数&#xff1a;object sender&#xff0c;EventArgs e。该事件使用这两个参数调用方法&#xff0c;因此我们不能直接添加自定义参数。 比如下面这段代码&#xff0c;我们想在 MessageBox 中显示字符串 s &#xff0c;这必然是不成。 priv…

赶紧收藏!2024 年最常见 100道 Java 基础面试题(十九)

上一篇地址&#xff1a;赶紧收藏&#xff01;2024 年最常见 100道 Java 基础面试题&#xff08;十八&#xff09;-CSDN博客 三十七、守护线程是什么&#xff1f; 守护线程&#xff08;Daemon Thread&#xff09;是Java中的一种特殊类型的线程&#xff0c;它的目的是为其他线程…

用过最佳的wordpress模板

西瓜红&#xff0c;作为一种充满活力和激情的颜色&#xff0c;总是能给人留下深刻的印象。当这种鲜艳的色彩与经典的设计元素相结合时&#xff0c;就能打造出一款既时尚又实用的WordPress企业模板。今天&#xff0c;我们向您隆重推荐这款西瓜红经典配色WordPress企业模板。 这…