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

R Programming week1-Subsetting的更多相关文章

  1. Coursera系列-R Programming第二周

    博客总目录,记录学习R与数据分析的一切:http://www.cnblogs.com/weibaar/p/4507801.html  --- 好久没发博客 且容我大吼一句 终于做完这周R Progra ...

  2. Coursera系列-R Programming第三周-词法作用域

    完成R Programming第三周 这周作业有点绕,更多地是通过一个缓存逆矩阵的案例,向我们示范[词法作用域 Lexical Scopping]的功效.但是作业里给出的函数有点绕口,花费了我们蛮多心 ...

  3. 让reddit/r/programming炸锅的一个帖子,还是挺有意思的

    这是原帖 http://www.reddit.com/r/programming/comments/358tnp/five_programming_problems_every_software_en ...

  4. [R] [Johns Hopkins] R Programming 作業 Week 2 - Air Pollution

    Introduction For this first programming assignment you will write three functions that are meant to ...

  5. R Programming week 3-Loop functions

    Looping on the Command Line Writing for, while loops is useful when programming but not particularly ...

  6. R programming, In ks.test(x, y) : p-value will be approximate in the presence of ties

    Warning message: In ks.test(x, y) : p-value will be approximate in the presence of ties   The warnin ...

  7. [R] [Johns Hopkins] R Programming -- week 3

    library(datasets) head(airquality) #按月分組 s <- split(airquality, airquality$Month) str(s) summary( ...

  8. [R] [Johns Hopkins] R Programming -- week 4

    #Generating normal distribution (Pseudo) random number x<-rnorm(10) x x2<-rnorm(10,2,1) x2 set ...

  9. R Programming week 3-Debugging

    Something’s Wrong! Indications that something’s not right message: A generic notification/diagnostic ...

随机推荐

  1. is和==的区别,小数据池,编码

    1   is  和  == 的区别 1>    id( )表示我们可以通过它来查到在内存中的地址 s = "alex" lst = [1,2, 4] lst = [1, 2, ...

  2. Django 使用UEditor

    Django package 的一些包不支持upload file, 而且 有几个支持的不是收费的就是要开csrf ,这对于苦逼程序猿来说始终是件恼火的事.所以经过查阅各种资料.看了各种各样的配置do ...

  3. jsonp突破同源策略,实现跨域訪问请求

    版权声明:本文为博主原创文章,未经博主同意不得转载.如需转载请声明:[转自 http://blog.csdn.net/xiaoxian8023 ] https://blog.csdn.net/xiao ...

  4. linux input子系统 — TP A/B(Slot)协议【转】

    本文转载自:http://blog.csdn.net/u012719256/article/details/53609906 将A/B协议这部分单独拿出来说一方面是因为这部分内容是比较容易忽视的,周围 ...

  5. shell系统管理

    背景知识 对于 Linux 系统管理员来说,没有比 shell 脚本编程更有用处的了.通常,Linux 系统管理员每天需要完成无数项任务,从监视系统磁盘空间和系统用户到备份重要文件.Shell 脚本可 ...

  6. 【SCOI 2005】 最大子矩阵

    [题目链接] 点击打开链接 [算法] 动态规划 我们发现,M只有两种取值,1和2,那么,只需分类讨论即可 当M = 1时,其实这个问题就成了就最大连续子段和的问题,只不过要选K段而已 用f[i][j] ...

  7. lua 与C通过c api传递table

    此文转自http://blog.csdn.net/perfect2011/article/details/19200511(感谢...) 首先了解下c++与lua之间的通信: 假设在一个lua文件中有 ...

  8. SpringMVC数据绑定三(JSON 、XML))

    dhc chrome 地址https://chrome.google.com/webstore/detail/restlet-client-rest-api-t/aejoelaoggembcahagi ...

  9. TCP/IP协议的建立连接与关闭连接过程

    一.建立连接(三次握手) 第一次握手:建立连接时,客户端发送SYN(seq=x)包到服务器,并进入SYN_SENT状态,等待服务器的确认.SYN:同步序列编号(Synchronize Sequence ...

  10. 线上项目mysql、redis平滑迁移方案及步骤

    1.清晰系统内网及公网可达,CVM配置 2.迁移完整数据,项目部署,测试网络环境. redis:复制rdb文件mysql:xtrabackup备份3.确保项目正常运行,网络正常访问.项目对外接口及账户 ...