Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
59 views8 pages

Vector Subsetting

This document discusses how to subset or extract elements from vectors in R using indexes, names, and logical vectors. It shows that vectors can be subset by integer index to extract a single element, by name to extract a named element, or using a logical or integer vector to extract multiple elements. Subsetting in R allows retrieving parts of a vector rather than the entire vector, and the position or names of the selected elements matters for the returned output.

Uploaded by

seggy7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views8 pages

Vector Subsetting

This document discusses how to subset or extract elements from vectors in R using indexes, names, and logical vectors. It shows that vectors can be subset by integer index to extract a single element, by name to extract a named element, or using a logical or integer vector to extract multiple elements. Subsetting in R allows retrieving parts of a vector rather than the entire vector, and the position or names of the selected elements matters for the returned output.

Uploaded by

seggy7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

INTRODUCTION TO R

Subsetting Vectors

Introduction to R

Subset by index
> remain <- c(spades = 11, hearts = 12,
diamonds = 11, clubs = 13)
> remain[1]
spades
11
> remain[3]
diamonds
11

[1] -> take element at index 1


result is a (named) vector too!

Introduction to R

Subset by name
> remain <- c(spades = 11, hearts = 12,
diamonds = 11, clubs = 13)
> remain["spades"]
spades
11
> remain["diamonds"]
diamonds
11

Introduction to R

Subset multiple elements


> remain <- c(spades = 11, hearts = 12,
diamonds = 11, clubs = 13)
> remain_black <- remain[c(1, 4)]
> remain_black
spades clubs
order in selection vector matters!
11
13
> remain[c(4, 1)]
clubs spades
13
11
> remain[c("clubs", "spades")]
clubs spades
13
11

Introduction to R

Subset all but some


> remain <- c(spades = 11, hearts = 12,
diamonds = 11, clubs = 13)
> remain[-1]
hearts diamonds
12
11

clubs
13

All but index 1 are returned

> remain[-c(1, 2)]


diamonds
clubs
11
13
> remain[-"spades"]
Error in -"spades" : invalid argument to unary operator

Introduction to R

Subset using logical vector


> remain <- c(spades = 11, hearts = 12,
diamonds = 11, clubs = 13)
> remain[c(FALSE, TRUE, FALSE, TRUE)]
hearts clubs
12
13
> selection_vector <- c(FALSE, TRUE, FALSE, TRUE)
> remain[selection_vector]
hearts clubs
12
13

Introduction to R

Subset using logical vector


> remain <- c(spades = 11, hearts = 12,
diamonds = 11, clubs = 13)
> remain[c(TRUE, FALSE)]
R recycles c(T, F) to c(T, F, T, F)
spades diamonds
11
11
> remain[c(TRUE, FALSE, TRUE, FALSE)]
spades diamonds
11
11
> remain[c(TRUE, FALSE, TRUE)]
spades diamonds
clubs
11
11
13
> remain[c(TRUE, FALSE, TRUE, TRUE)]
spades diamonds
clubs
11
11
13

INTRODUCTION TO R

Lets practice!

You might also like