原文地址:https://www.statmethods.net/management/subset.html

R has powerful indexing features for accessing object elements. These features can be used to select and exclude variables and observations. The following code snippets demonstrate ways to keep or delete variables and observations and to take random samples from a dataset.

Selecting (Keeping) Variables

# select variables v1, v2, v3
myvars <- c("v1", "v2", "v3")
newdata <- mydata[myvars]

# another method
myvars <- paste("v", 1:3, sep="")
newdata <- mydata[myvars]

# select 1st and 5th thru 10th variables
newdata <- mydata[c(1,5:10)]

To practice this interactively, try the selection of data frame elements exercises in the Data frames chapter of this introduction to R course.

Excluding (DROPPING) Variables

# exclude variables v1, v2, v3
myvars <- names(mydata) %in% c("v1", "v2", "v3") 
newdata <- mydata[!myvars]

# exclude 3rd and 5th variable 
newdata <- mydata[c(-3,-5)]

# delete variables v3 and v5
mydata$v3 <- mydata$v5 <- NULL

Selecting Observations

# first 5 observations
newdata <- mydata[1:5,]

# based on variable values
newdata <- mydata[ which(mydata$gender=='F' 
& mydata$age > 65), ]

# or
attach(mydata)
newdata <- mydata[ which(gender=='F' & age > 65),]
detach(mydata)

Selection using the Subset Function

The subset( ) function is the easiest way to select variables and observations. In the following example, we select all rows that have a value of age greater than or equal to 20 or age less then 10. We keep the ID and Weight columns.

# using subset function 
newdata <- subset(mydata, age >= 20 | age < 10, 
select=c(ID, Weight))

In the next example, we select all men over the age of 25 and we keep variables weight through income (weight, income and all columns between them).

# using subset function (part 2)
newdata <- subset(mydata, sex=="m" & age > 25,
select=weight:income)

To practice the subset() function, try this this interactive exercise. on subsetting data.tables.

Random Samples

Use the sample( ) function to take a random sample of size n from a dataset.

# take a random sample of size 50 from a dataset mydata 
# sample without replacement
mysample <- mydata[sample(1:nrow(mydata), 50,
   replace=FALSE),]

转载于:https://www.cnblogs.com/chickenwrap/p/10166562.html

R语言文摘:Subsetting Data相关推荐

  1. R语言dataframe(data.table)使用用最近的前一个非NA值向前填充缺失值NA实战

    R语言dataframe(data.table)使用用最近的前一个非NA值向前填充缺失值NA实战 目录 R语言dataframe(data.tabl

  2. R语言数据整理Data Tidying(基于tidyr包)

    很多数据一拿来并不是整齐的,不适合让计算机来作数据分析,因此需要对数据进行各种处理,来让数据变得"tidy". 下面,基于R语言的tidyr包的一些函数来对原始数据进行整理. *( ...

  3. 【R语言】对data.frame的行随机抽样

    df是R中的data.frame对象,对其行进项无放回随机采样,直接使用以下代码即可: df[sample(nrow(df), 10), ]

  4. R语言数据框data.frame行和列求和方法

    代码如下: widgets <- c(179,153,183,153,154) gadgets <- c(167,193,190,161,181) thingys <- c(182, ...

  5. R语言入门第六集 实验五:综合应用

    R语言入门第六集 实验五:综合应用 一.资源 [R语言]沈阳地铁数据处理及站间流量统计--R语言第五次实训 lubridate-轻松处理日期时间 数据整理-dplyr包(mutate系列) CEILI ...

  6. go语言csv包_玩转数据处理120题R语言版本

    点击上方"早起Python",关注并星标公众号 和我一起玩Python 本文为玩转数据处理120题|R语言版本 习题|刘早起,解答|陈熹 大家好,本文为R语言数据处理120题系列完 ...

  7. R语言学习手记 (1)

    R语言学习手记 (1) 经管的会计和财管都会学数据统计与分析R语言这门课,加上我也有点兴趣,就提前选了这门课,以下的笔记由老师上课的PPT.<R语言编程艺术>和<R语言数据科学> ...

  8. 广义相加模型(GAM)与向前逐步选择算法(基于R语言)

    广义相加模型(GAM)与向前逐步选择算法(基于R语言) 一.题目 (a)使用College数据集,以Outstate作为响应变量,其余作为预测变量,使用逐步回归得到一组合适的预测变量的子集. (b)将 ...

  9. 单个总体的均值向量的检验(R语言)

    ▼单个总体的均值向量的检验▼ 描述: 1.作统计假设:H0:μ=μ0,H1:μ≠μ0 2.计算样本的均值 3.计算统计量的具体值: 4.按规定的小概率标准α,查卡方分布表,得临界值,并做出判断: 当  ...

最新文章

  1. Linux内存中的 buffer 和 cache 到底是个什么东东?
  2. 5G NGC — eNS(增强的网络切片)
  3. android 自定义模板下载,android studio 自定义模板
  4. 一次内网靶场学习记录
  5. 设计模式:装饰模式(Decorator)
  6. 前端学习(3039):vue+element今日头条管理-侧边菜单栏的展示和收缩
  7. 滤波器的优点_声光可调谐滤波器
  8. 解决xx is not in the sudoers file. This incident will be reported.问题
  9. 前端校验rules写法:
  10. android studio页面布局代码,还在用Android正经布局来写页面吗?
  11. Infragistics NetAdvantage UltraGrid的使用
  12. 【Jenkins】构建后生成下载二维码并邮件通知
  13. winform 线程 句柄不断增加_多线程讲解
  14. 一句话说明sync, fsync, fdatasync的区别
  15. 来了,来了,他来了,使用Github制作自己的在线简历(网页和PDF版)你都值得拥有
  16. 计算机辅助英语教学 研究背景,精选:信息时代背景下的英语教学原稿
  17. 家谱管理系统php,家谱管理系统(含源代码).docx
  18. java多表头导出excel表格_【每日一点】1. Java如何实现导出Excel单表头或多表头
  19. funny pics
  20. cscd期刊是c刊吗_武工商C刊和北大核心期刊论文发表数量位列全省同类高校前三甲...

热门文章

  1. 朴素高精度乘法的常数优化
  2. 编译u-boot时候,make distclean 出现rm:无法删除,****是一个目录
  3. 用Asp.net实现简单的文字水印
  4. 《40期》 我们要把世纪末日变成重生日
  5. F5负载均衡会话保持技术及原理技术白皮书
  6. 跨站脚本攻击(XSS)FAQ
  7. Ajax实现DataGrid/DataList动态ToolTip
  8. Repeater控件的分页问题
  9. 码农技术炒股之路——实时交易信息、主力动向信息分库备份
  10. matlab内存管理