R Is Not So Hard! A Tutorial, Part 9: Sub-setting

In Part 9, let’s look at sub-setting in R. I want to show you two approaches.

Let’s provide summary tables on the following data set of tourists from different nations, their gender and numbers of children. Copy and paste the following array into R.

A<- structure(list(NATION = structure(c(3L, 3L, 3L, 1L, 3L, 2L, 3L,
1L, 3L, 3L, 1L, 2L, 2L, 3L, 3L, 3L, 2L), .Label = c("CHINA",
"GERMANY", "FRANCE"), class = "factor"), GENDER = structure(c(1L,
2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L
), .Label = c("F", "M"), class = "factor"), CHILDREN = c(1L,
3L, 2L, 2L, 3L, 1L, 0L, 1L, 0L, 1L, 2L, 2L, 1L, 1L, 1L, 0L, 2L
)), .Names = c("NATION", "GENDER", "CHILDREN"), row.names = 2:18, class = "data.frame")
A

The generic form of the syntax we will use is as follows:

Z <- A[ A[ , column ] == val, ]

Note that we have two sets of square brackets and a comma just before the second closing bracket. Z gives all rows for which an indicator in column column has the value val. We can say it like this: “Z is the set of rows of A such that the elements of column column have the value val”.

OK. Let’s subset for females.

FE<- A[ A[, 2] == "F", ]
FE

 
   NATION   GENDER  CHILDREN
2  FRANCE     F        1
5  CHINA      F        2
7  GERMANY    F        1
9  CHINA      F        1
12 CHINA      F        2
13 GERMANY    F        2
14 GERMANY    F        1
15 FRANCE     F        1
17 FRANCE     F        0
18 GERMANY    F        2

However, easier is the following syntax, using the subset function:
FE <- subset(A, GENDER == "F")
FE

Now let’s subset fortourists for which the third column (number of children) is less than 2.

C1<- A[ A[, 3] <2, ]
C1

 
   NATION  GENDER CHILDREN
2  FRANCE     F      1
7  GERMANY    F      1
8  FRANCE     M      0
9  CHINA      F      1
10 FRANCE     M      0
11 FRANCE     M      1
14 GERMANY    F      1
15 FRANCE     F      1
16 FRANCE     M      1
17 FRANCE     F      0

Using the subset function . . .

C1<- subset(A, CHILDREN <2)
C1

Finally, we isolate all rows for Females with less than two children.

F1<- A[ A[, 2] == "F"&A[, 3] <2, ]
F1

Using the subset function . . .

F1<- subset(A, GENDER == "F"&CHILDREN <2)
F1

You can practice more of these yourself by using the comparison operators:

== (equal to)

!= (not equal to) > (greater than) < (less than)

>= (greater than or equal to) <= (less than or equal to)

 

Also note the logical operators: & (and) | (or) ! (not)

That wasn’t so hard! In blog 10 we will look at further analytic techniques in R.

About the Author: David Lillis has taught R to many researchers and statisticians. His company, Sigma Statistics and Research Limited, provides both on-line instruction and face-to-face workshops on R, and coding services in R. David holds a doctorate in applied statistics.

See our full R Tutorial Series and other blog posts regarding R programming.

 

Getting Started with R
Kim discusses the use of R statistical software for data manipulation, calculation, and graphical display.

Reader Interactions

Comments

  1. JFS says

    The final “M” in the input–is it for anything, or a typo? I can’t figure what it’d be for, and it doesn’t read in right. When copy-pasting, there’s an invisible carriage return, and it goes in as its own (apparently meaningless) command, generating the response, “Error: object ‘M’ not found.” If I edit the invisible CR to tag the “M” onto the previous line, either with or without a space, the error message is “Error: unexpected symbol in:
    “3L, 2L, 2L, 3L, 1L, 0L, 1L, 0L, 1L, 2L, 2L, 1L, 1L, 1L, 0L, 2L
    )), .Names = c(“NATION”, “GENDER”, “CHILDREN”), row.names = 2:18, class = “data.frame”) M” (with or without a space before the “M”).


Leave a Reply

Your email address will not be published. Required fields are marked *

Please note that, due to the large number of comments submitted, any questions on problems related to a personal study/project will not be answered. We suggest joining Statistically Speaking, where you have access to a private forum and more resources 24/7.