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. win系统下启动linux上的kafka集群及使用

    一.首先在win系统下C:\Windows\System32\drivers\etc文件夹中hosts文件加入例如以下内容: 10.61.6.167 slaves1 10.61.6.168 slave ...

  2. Xcode 中的小技巧

    显示或隐藏欢迎页面 Command + Shift + 1 显示欢迎页 假设不想每次打开都显示,能够将上图中的勾去掉

  3. NYOJ1026 阶乘末尾非0 【模板】

    阶乘末尾非0 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描写叙述 我们的问题非常是简单.n! 末尾非0数是几? 比方n=5的时候,n! =120,那么n!末尾非0数是2. ...

  4. ps -ef | grep

    ps -ef | grep java   查看所有关于java的进程 root     17540     1  0  2009 ?        01:42:27 /usr/java/jdk1.5. ...

  5. camera摄像原理之四:曝光和GAMMA

    GAMMA:输出/输入(光信号值) 从最明亮到最黑暗,假设人眼能够看到一定的范围,那么胶片(或CCD 等电子感光器件)所能表现的远比人眼看到的范围小的多,而这个有限的范围就是感光宽容度. 人眼的感光宽 ...

  6. POJ1984 Navigation Nightmare —— 种类并查集

    题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K T ...

  7. 一场由过滤器Filter引发的血案

    一场由过滤器Filter引发的血案 事件起因 本来应该是下图的登录界面 变成了这样 What's the fuck????? 抓狂 原因 解决方法: 在过滤器中给资源文件开个绿色通道

  8. [Django基础] django解决静态文件依赖问题以及前端引入方式

    一.静态文件依赖 学习django的时候发现静态文件(css,js等)不能只在html中引入,还要在项目的settings中设置,否则会报以下错误 [11/Sep/2018 03:18:15] &qu ...

  9. IJ:IJ来了

    ylbtech-IJ:IJ来了 1.返回顶部 1. 2. 2.2. 3. 4. 2.返回顶部 1. 2. 3. 3. Welcome to IntelliJ IDEA返回顶部 1. 2.2. 2.2. ...

  10. 反编译Android的apk包得到源码(使用工具:dex2jar和jd-gui)

    1. 先从 http://download.csdn.net/detail/dingyuming1991/9618125 下载反编译工具dex2jar和jd-gui(当然也可以google搜索下载): ...