• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Analysis Factor

The Analysis Factor

Statistical Consulting, Resources, and Statistics Workshops for Researchers

  • Home
  • About
    • Our Programs
    • Our Team
    • Our Core Values
    • Our Privacy Policy
    • Employment
    • Guest Instructors
  • Membership
    • Statistically Speaking Membership Program
    • Login
  • Workshops
    • Online Workshops
    • Login
  • Consulting
    • Statistical Consulting Services
    • Login
  • Free Webinars
  • Contact
  • Login

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

by guest 1 Comment

by David Lillis, Ph.D.

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")
M

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

Z <- A[ A[ , colnum ] == 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

NATIONGENDER 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

NATIONGENDER 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.

Bookmark and Share

Tagged With: programming, R, sub-setting

Related Posts

  • R Is Not So Hard! A Tutorial, Part 18: Re-Coding Values
  • R Is Not So Hard! A Tutorial, Part 11: Creating Bar Charts
  • R Is Not So Hard! A Tutorial, Part 8: Basic Commands
  • The Advantages of RStudio

Reader Interactions

Comments

  1. JFS says

    February 1, 2017 at 12:10 pm

    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”).

    Reply

Leave a Reply Cancel 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.

Primary Sidebar

This Month’s Statistically Speaking Live Training

  • January Member Training: A Gentle Introduction To Random Slopes In Multilevel Models

Upcoming Workshops

  • Logistic Regression for Binary, Ordinal, and Multinomial Outcomes (May 2021)
  • Introduction to Generalized Linear Mixed Models (May 2021)

Read Our Book



Data Analysis with SPSS
(4th Edition)

by Stephen Sweet and
Karen Grace-Martin

Statistical Resources by Topic

  • Fundamental Statistics
  • Effect Size Statistics, Power, and Sample Size Calculations
  • Analysis of Variance and Covariance
  • Linear Regression
  • Complex Surveys & Sampling
  • Count Regression Models
  • Logistic Regression
  • Missing Data
  • Mixed and Multilevel Models
  • Principal Component Analysis and Factor Analysis
  • Structural Equation Modeling
  • Survival Analysis and Event History Analysis
  • Data Analysis Practice and Skills
  • R
  • SPSS
  • Stata

Copyright © 2008–2021 The Analysis Factor, LLC. All rights reserved.
877-272-8096   Contact Us

The Analysis Factor uses cookies to ensure that we give you the best experience of our website. If you continue we assume that you consent to receive cookies on all websites from The Analysis Factor.
Continue Privacy Policy
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Non-necessary

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.