文档章节

R Programming week1-Subsetting

hyclsj
 hyclsj
发布于 2015/04/17 11:10
字数 676
阅读 3
收藏 0

Subsetting

There are a number of operators that can be used to extract subsets of R objects.

[ always returns an object of the same class as the original; can be used to select more than one

element (there is one exception)

[[ is used to extract elements of a list or a data frame; it can only be used to extract a single

element and the class of the returned object will not necessarily be a list or data frame

$ is used to extract elements of a list or data frame by name; semantics are similar to that of [[.

> x <- c("a", "b", "c", "c", "d", "a")

> x[1]

[1] "a"

> x[2]

[1] "b"

> x[1:4]

[1] "a" "b" "c" "c"

> x[x > "a"]

[1] "b" "c" "c" "d"

> u <- x > "a"

> u

[1] FALSE TRUE TRUE TRUE TRUE FALSE

> x[u]

[1] "b" "c" "c" "d"

Subsetting Lists

> x <- list(foo = 1:4, bar = 0.6)

> x[1]

$foo

[1] 1 2 3 4

> x[[1]]

[1] 1 2 3 4

> x$bar

[1] 0.6

> x[["bar"]]

[1] 0.6

> x["bar"]

$bar

[1] 0.6

> x <- list(foo = 1:4, bar = 0.6, baz = "hello")

> x[c(1, 3)]

$foo

[1] 1 2 3 4

$baz

[1] "hello"

The [[ operator can be used with computed indices; $ can only be used with literal names.

> x <- list(foo = 1:4, bar = 0.6, baz = "hello")

> name <- "foo"

> x[[name]] ## computed index for ‘foo’

[1] 1 2 3 4

> x$name ## element ‘name’ doesn’t exist!

NULL

> x$foo

[1] 1 2 3 4 ## element ‘foo’ does exist

Subsetting Nested Elements of a List

The [[ can take an integer sequence

> x <- list(a = list(10, 12, 14), b = c(3.14, 2.81))

> x[[c(1, 3)]]

[1] 14

> x[[1]][[3]]

[1] 14

> x[[c(2, 1)]]

[1] 3.14

Subsetting a Matrix

Matrices can be subsetted in the usual way with (i,j) type indices.

> x <- matrix(1:6, 2, 3)

> x[1, 2]

[1] 3

> x[2, 1]

[1] 2

Indices can also be missing.

> x[1, ]

[1] 1 3 5

> x[, 2]

[1] 3 4

By default, when a single element of a matrix is retrieved, it is returned as a vector of length 1 rather than a 1 × 1 matrix. This behavior can be turned off by setting drop = FALSE.

> x <- matrix(1:6, 2, 3)

> x[1, 2]

[1] 3

> x[1, 2, drop = FALSE]

 [,1]

[1,] 3

Similarly, subsetting a single column or a single row will give you a vector, not a matrix (by default).

> x <- matrix(1:6, 2, 3)

> x[1, ]

[1] 1 3 5

> x[1, , drop = FALSE]

 [,1] [,2] [,3]

[1,] 1 3 5

Partial Matching

Partial matching of names is allowed with [[ and $.

> x <- list(aardvark = 1:5)

> x$a

[1] 1 2 3 4 5

> x[["a"]]

NULL

> x[["a", exact = FALSE]]

[1] 1 2 3 4 5

Removing NA Values

A common task is to remove missing values (NAs).

> x <- c(1, 2, NA, 4, NA, 5)

> bad <- is.na(x)

> x[!bad]

[1] 1 2 4 5

What if there are multiple things and you want to take the subset with no missing values?

> x <- c(1, 2, NA, 4, NA, 5)

> y <- c("a", "b", NA, "d", NA, "f")

> good <- complete.cases(x, y)

> good

[1] TRUE TRUE FALSE TRUE FALSE TRUE

> x[good]

[1] 1 2 4 5

> y[good]

[1] "a" "b" "d" "f"

 

> airquality[1:6, ]

 Ozone Solar.R Wind Temp Month Day

1 41 190 7.4 67 5 1

2 36 118 8.0 72 5 2

3 12 149 12.6 74 5 3

4 18 313 11.5 62 5 4

5 NA NA 14.3 56 5 5

6 28 NA 14.9 66 5 6

> good <- complete.cases(airquality)

> airquality[good, ][1:6, ]

 Ozone Solar.R Wind Temp Month Day

1 41 190 7.4 67 5 1

2 36 118 8.0 72 5 2

3 12 149 12.6 74 5 3

4 18 313 11.5 62 5 4

7 23 299 8.6 65 5 7

© 著作权归作者所有

hyclsj
粉丝 0
博文 7
码字总数 8867
作品 0
武汉
程序员
私信 提问
单用户/救援模式、虚拟机克隆、Linux间互登录

本文索引 单用户模式 开关机命令 修改root密码 救援模式 准备 修改root密码 克隆虚拟机 操作步骤 修改参数 Linux机器间互相登录 --- 单用户模式 开关机命令 重启服务器 reboot init 6 shutdo...

Lu_Castiel
2017/10/18
12
0
Calibre 2.31 发布,电子书管理软件

Calibre 2.31 发布,更新内容如下: 新特性 E-book viewer: Make the search engine used for searching for selected text configurable (in the Miscellaneous section of the viewer pref......

oschina
2015/06/19
1K
0
Data Carpentry Workshop - Day 2 -Working with tabular data in R

Main Contents Describe what a data frame is. Load external data from a .csv file into a data frame. Summarize the contents of a data frame. Describe what a factor is. Convert be......

猪猪头看世界
2018/06/22
0
0
deep learning博客索引

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/robinXushuai/article/details/80686787 Notes of the specilization about deeplearning.ai provided by And......

_席达_
2018/06/15
0
0
Course2-week1-setting up your ML application

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/robinXushuai/article/details/80624763 setting up your ML application 1 - train/dev/test set This week ......

_席达_
2018/06/08
0
0

没有更多内容

加载失败,请刷新页面

加载更多

CentOS7.6中安装使用fcitx框架

内容目录 一、为什么要使用fcitx?二、安装fcitx框架三、安装搜狗输入法 一、为什么要使用fcitx? Gnome3桌面自带的输入法框架为ibus,而在使用ibus时会时不时出现卡顿无法输入的现象。 搜狗和...

技术训练营
昨天
5
0
《Designing.Data-Intensive.Applications》笔记 四

第九章 一致性与共识 分布式系统最重要的的抽象之一是共识(consensus):让所有的节点对某件事达成一致。 最终一致性(eventual consistency)只提供较弱的保证,需要探索更高的一致性保证(stro...

丰田破产标志
昨天
8
0
docker 使用mysql

1, 进入容器 比如 myslq1 里面进行操作 docker exec -it mysql1 /bin/bash 2. 退出 容器 交互: exit 3. mysql 启动在容器里面,并且 可以本地连接mysql docker run --name mysql1 --env MY...

之渊
昨天
10
0
python数据结构

1、字符串及其方法(案例来自Python-100-Days) def main(): str1 = 'hello, world!' # 通过len函数计算字符串的长度 print(len(str1)) # 13 # 获得字符串首字母大写的...

huijue
昨天
6
0
PHP+Ajax微信手机端九宫格抽奖实例

PHP+Ajax结合lottery.js制作的一款微信手机端九宫格抽奖实例,抽奖完成后有收货地址添加表单出现。支持可以设置中奖概率等。 奖品列表 <div class="lottery_list clearfix" id="lottery"> ......

ymkjs1990
昨天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部