前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R Programming-week1 Subsetting

R Programming-week1 Subsetting

作者头像
统计学家
发布2019-04-10 17:17:51
3290
发布2019-04-10 17:17:51
举报

Subsetting

Thereare 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 toselect more than one

element(there is one exception)

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

elementand the class of the returned object will not necessarily be a list or dataframe

$ isused to extract elements of a list or data frame by name; semantics are similarto 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 TRUETRUE 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 literalnames.

> 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 NestedElements 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 aMatrix

Matricescan 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

Indicescan also be missing.

> x[1, ]

[1] 1 3 5

> x[, 2]

[1] 3 4

Bydefault, when a single element of a matrix is retrieved, it is returned as avector of length 1 rather than a 1 × 1 matrix. This behavior can be turned offby 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

Partialmatching 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 NAValues

Acommon 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

Whatif there are multiple things and you want to take the subset with no missingvalues?

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

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

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

> good

[1] TRUE TRUEFALSE TRUE FALSE TRUE

> x[good]

[1] 1 2 4 5

> y[good]

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

>airquality[1:6, ]

OzoneSolar.R Wind Temp Month Day

1 41 190 7.4 67 51

2 36 118 8.0 72 52

3 12 149 12.6 745 3

4 18 313 11.5 625 4

5 NA NA 14.3 56 55

6 28 NA 14.9 66 56

> good <-complete.cases(airquality)

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

OzoneSolar.R Wind Temp Month Day

1 41 190 7.4 67 51

2 36 118 8.0 72 52

3 12 149 12.6 745 3

4 18 313 11.5 625 4

7 23 299 8.6 65 57

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2015-04-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 机器学习与统计学 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档