• 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
  • Our Programs
    • Membership
    • Online Workshops
    • Free Webinars
    • Consulting Services
  • About
    • Our Team
    • Our Core Values
    • Our Privacy Policy
    • Employment
    • Collaborate with Us
  • Statistical Resources
  • Contact
  • Blog
  • Login

R Is Not So Hard! A Tutorial, Part 8: Basic Commands

by guest contributer Leave a Comment

by David Lillis, Ph.D.

Let’s look at some basic commands in R.

Set up the following vector by cutting and pasting from this document:

a <- c(3,-7,-3,-9,3,-1,2,-12, -14)
b <- c(3,7,-5, 1, 5,-6,-9,16, -8)
d <- c(1,2,3,4,5,6,7,8,9)

Now figure out what each of the following commands do. You should not need me to explain each command, but I will explain a few.

a*a
a + b
a - d
a / d
a*d
a^2
a[7]
a[c(1,4,7)]
a[-c(5,6)]
length(a)
a[length(a)]
a[-length(a)]
length(a[c(1, 2, 3)])
a[8]
a[c(7, 5, 3)]
a>2
a[a >2]
a[-c(3, 5, 6)]
a^2
a**2
a + 99
min(b)
min(a,b,d)
max(a,b,d)
max(b)
mean(a)
median(d)
range(a)
unique(a,b, d)
length(unique(a,b, d))

The syntax a > 2 gives a list of values that are either TRUE or FALSE depending on whether each value meets the criterion of being greater than 2.

This list of values is called a vector, but don’t let the mathematical language intimidate you.  It’s just a named list.

Then the syntax a[a >2]subsets the vector a to a smaller set of values that are indeed greater than 2.

Again, the data values in the list are called elements.

You can see that these elements are identified and indexed using square brackets. Elements are retained if the indices are positive, and excluded if the indices are negative. Note that a[length(a)] picks out the last element in the list, while a[-length(a)] removes the last element. Of course, length(a)gives the number of elements in the vector a.

Suppose that you wished to remove the last element of a vector permanently, you could do this:

a <- a[-length(a)]
a
[1] 3 -7 -3 -9 3 -1 2 -12

The last element (-14) has been removed.

That wasn’t so hard! In my next post, I will look at further statistical 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.

Bookmark and Share

Tagged With: R

Related Posts

  • R Is Not So Hard! A Tutorial, Part 18: Re-Coding Values
  • R Is Not So Hard! A Tutorial, Part 9: Sub-setting
  • R is Not So Hard! A Tutorial, Part 22: Creating and Customizing Scatter Plots
  • R is Not So Hard! A Tutorial, Part 21: Pearson and Spearman Correlation

Reader Interactions

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

  • Member Training: Analyzing Pre-Post Data

Upcoming Free Webinars

Poisson and Negative Binomial Regression Models for Count Data

Upcoming Workshops

  • Analyzing Count Data: Poisson, Negative Binomial, and Other Essential Models (Jul 2022)
  • Introduction to Generalized Linear Mixed Models (Jul 2022)

Copyright © 2008–2022 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.
SAVE & ACCEPT