• 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 16: Counting Values within Cases

by guest Leave a Comment

by David Lillis, Ph.D.

SPSS has the Count Values within Cases option, but R does not have an equivalent function. Here are two functions that you might find helpful, each of which counts values within cases inside a rectangular array.

For example, you might have a data set consisting of responses to a questionnaire involving multiple Likert items scored 1 to 5. You may wish to know the number of items for which respondents selected a 5.

Note the syntax for creating a function, which I will not discuss here. Copy and paste the two functions into the R workspace. They both do the same job, so that you can choose either of them for your own data analysis.

countcases1 <- function(x, n) { apply(x, 1, function(r) sum(r == n)) } 

OR

countcases2 <- function(x, n) { rowSums(x == n) }

Now let’s use these functions to count elements within a rectangular array. Let’s use the following array M.

M <- structure(c(1, 4, 5, 4, 5, 5, 3, 2, 5, 5, 5, 4, 5, 2, 5), .Dim = c(3L, 5L), .Dimnames = list(c("David", "Mary", "Anne"), NULL))

colnames(M) <- c("Item1", "Item2", "Item3", "Item4", "Item5")

M

       Item1  Item2 Item3 Item4 Item5
David     1     4     3     5     5
Mary      4     5     2     5     2
Anne      5     5     5     4     5

Let’s count the fives along the rows of M.

countcases1(M, 5)

 David  Mary  Anne
    2     2     4

Let’s use the other function to count the twos.

countcases2(M, 2)

David  Mary  Anne
    0     2     0

Each of the two functions has produced a vector of counts. Now let’s pick out the number of fives in the FIRST row using square brackets.

countcases1(M, 5)[1]

David
    2

OR:

countcases1(M, 5)["David"]

David
    2

To see the rest of the R is Not So Hard! tutorial series, visit our R Resource page.

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

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

Tagged With: counting, R, values

Related Posts

  • 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
  • R Graphics: Plotting in Color with qplot Part 2
  • Doing Scatterplots in R

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

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