Summarizing a factor
After finishing this course, one of your favorite functions in R will be summary()
. This will give you a quick overview of the contents of a variable:
summary(my_var)
Going back to our survey, you would like to know how many "Male"
responses you have in your study, and how many "Female"
responses. The summary
function gives you the answer to this question.
This is a part of the course
“Introduction to R”
Exercise instructions
Ask a summary()
of the survey_vector
and factor_survey_vector
. Interpret the results of both vectors. Are they both equally useful in this case?
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Build factor_survey_vector with clean levels
survey_vector <- c("M", "F", "F", "M", "M")
factor_survey_vector <- factor(survey_vector)
levels(factor_survey_vector) <- c("Female", "Male")
factor_survey_vector
# Generate summary for survey_vector
# Generate summary for factor_survey_vector
This exercise is part of the course
Introduction to R
Master the basics of data analysis in R, including vectors, lists, and data frames, and practice R with real data sets.
Chapter 1: Intro to basics
Take your first steps with R. In this chapter, you will learn how to use the console as a calculator and how to assign variables. You will also get to know the basic data types in R. Let's get started.
Exercise 1: How it worksExercise 2: Arithmetic with RExercise 3: Variable assignmentExercise 4: Variable assignment (2)Exercise 5: Variable assignment (3)Exercise 6: Apples and orangesExercise 7: Basic data types in RExercise 8: What's that data type?Chapter 2: Vectors
We take you on a trip to Vegas, where you will learn how to analyze your gambling results using vectors in R. After completing this chapter, you will be able to create vectors in R, name them, select elements from them, and compare different vectors.
Exercise 1: Create a vectorExercise 2: Create a vector (2)Exercise 3: Create a vector (3)Exercise 4: Naming a vectorExercise 5: Naming a vector (2)Exercise 6: Calculating total winningsExercise 7: Calculating total winnings (2)Exercise 8: Calculating total winnings (3)Exercise 9: Comparing total winningsExercise 10: Vector selection: the good timesExercise 11: Vector selection: the good times (2)Exercise 12: Vector selection: the good times (3)Exercise 13: Vector selection: the good times (4)Exercise 14: Selection by comparison - Step 1Exercise 15: Selection by comparison - Step 2Exercise 16: Advanced selectionChapter 3: Matrices
In this chapter, you will learn how to work with matrices in R. By the end of the chapter, you will be able to create matrices and understand how to do basic computations with them. You will analyze the box office numbers of the Star Wars movies and learn how to use matrices in R. May the force be with you!
Exercise 1: What's a matrix?Exercise 2: Analyze matrices, you shallExercise 3: Naming a matrixExercise 4: Calculating the worldwide box officeExercise 5: Adding a column for the Worldwide box officeExercise 6: Adding a rowExercise 7: The total box office revenue for the entire sagaExercise 8: Selection of matrix elementsExercise 9: A little arithmetic with matricesExercise 10: A little arithmetic with matrices (2)Chapter 4: Factors
Data often falls into a limited number of categories. For example, human hair color can be categorized as black, brown, blond, red, grey, or white—and perhaps a few more options for people who color their hair. In R, categorical data is stored in factors. Factors are very important in data analysis, so start learning how to create, subset, and compare them now.
Exercise 1: What's a factor and why would you use it?Exercise 2: What's a factor and why would you use it? (2)Exercise 3: What's a factor and why would you use it? (3)Exercise 4: Factor levelsExercise 5: Summarizing a factorExercise 6: Battle of the sexesExercise 7: Ordered factorsExercise 8: Ordered factors (2)Exercise 9: Comparing ordered factorsChapter 5: Data frames
Most datasets you will be working with will be stored as data frames. By the end of this chapter, you will be able to create a data frame, select interesting parts of a data frame, and order a data frame according to certain variables.
Exercise 1: What's a data frame?Exercise 2: Quick, have a look at your datasetExercise 3: Have a look at the structureExercise 4: Creating a data frameExercise 5: Creating a data frame (2)Exercise 6: Selection of data frame elementsExercise 7: Selection of data frame elements (2)Exercise 8: Only planets with ringsExercise 9: Only planets with rings (2)Exercise 10: Only planets with rings but shorterExercise 11: SortingExercise 12: Sorting your data frameChapter 6: Lists
As opposed to vectors, lists can hold components of different types, just as your to-do lists can contain different categories of tasks. This chapter will teach you how to create, name, and subset these lists.
Exercise 1: Lists, why would you need them?Exercise 2: Lists, why would you need them? (2)Exercise 3: Creating a listExercise 4: Creating a named listExercise 5: Creating a named list (2)Exercise 6: Selecting elements from a listExercise 7: Creating a new list for another movieWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.