1. First steps in R and RStudio
Learning goals
After this lesson, you should be able to:
- explain the difference between R and RStudio;
- open an R project and run a script;
- create objects using
<-; - import a CSV file;
- inspect rows, columns, and variable types;
- find help when you do not remember a function.
Research question: What information is contained in our employee survey, and how is the dataset organized?
R and RStudio
R performs the calculations. RStudio is the interface in which we write, run, organize, and inspect R code.
A useful beginner rule is:
Write important work in a script, not only in the Console.
Console commands disappear easily. A saved script records what you did and can be run again.
Open the project
Open r-for-management-research.Rproj. Then open code/01-first-steps.R.
Load a package
A package adds functions to R. tidyverse contains tools for importing, transforming, and visualizing data.
Import the dataset
employees <- read_csv(
"data/employee_survey.csv",
show_col_types = FALSE
)Read this from right to left:
-
read_csv()reads the file; - the result is assigned to the object
employees; - the object can be reused in later commands.
Inspect the data
head(employees)# A tibble: 6 × 26
employee_id age gender department remote_days tenure_years manager
<chr> <dbl> <chr> <chr> <dbl> <dbl> <chr>
1 E000001 37 Man Operations 3 9.2 No
2 E000002 32 Man Operations 3 7 No
3 E000003 22 Man Operations 1 0.7 No
4 E000004 44 Woman Technology 1 4.2 No
5 E000005 42 Woman Operations 3 7.4 No
6 E000006 41 Woman Technology 0 1.6 Yes
# ℹ 19 more variables: training_group <chr>, leadership_1 <dbl>,
# leadership_2 <dbl>, leadership_3 <dbl>, leadership_4_reverse <dbl>,
# engagement_1 <dbl>, engagement_2 <dbl>, engagement_3 <dbl>,
# engagement_4_reverse <dbl>, role_clarity_1 <dbl>, role_clarity_2 <dbl>,
# role_clarity_3 <dbl>, role_clarity_4 <dbl>, workload <dbl>,
# organizational_support <dbl>, job_satisfaction <dbl>, performance <dbl>,
# burnout <dbl>, turnover_intention <dbl>
glimpse(employees)Rows: 480
Columns: 26
$ employee_id <chr> "E000001", "E000002", "E000003", "E000004", "E0…
$ age <dbl> 37, 32, 22, 44, 42, 41, 48, 23, 32, 25, 51, 39,…
$ gender <chr> "Man", "Man", "Man", "Woman", "Woman", "Woman",…
$ department <chr> "Operations", "Operations", "Operations", "Tech…
$ remote_days <dbl> 3, 3, 1, 1, 3, 0, 3, 0, 3, 4, 2, 0, 4, 1, 2, 2,…
$ tenure_years <dbl> 9.2, 7.0, 0.7, 4.2, 7.4, 1.6, 1.6, 2.9, 10.0, 6…
$ manager <chr> "No", "No", "No", "No", "No", "Yes", "No", "Yes…
$ training_group <chr> "Training", "Training", "Control", "Training", …
$ leadership_1 <dbl> 1, 3, 5, 3, 3, 2, 3, 3, 5, 2, 4, 5, 1, 2, 1, 2,…
$ leadership_2 <dbl> 1, 1, 5, 2, 4, 3, 3, 4, 5, 1, 5, 5, 1, 1, 1, 2,…
$ leadership_3 <dbl> 1, 1, 5, 3, 5, 4, 4, 2, 5, 2, 5, 5, 1, 1, 2, 2,…
$ leadership_4_reverse <dbl> 5, 5, 1, 4, 1, 3, 3, 4, 1, 5, 2, 1, 3, 4, 4, 3,…
$ engagement_1 <dbl> 2, 3, 5, 4, 5, 3, 5, 2, 5, 3, 4, 5, 3, 2, 4, 5,…
$ engagement_2 <dbl> 2, 1, 3, 5, 4, 4, 5, 1, 5, 2, 4, 5, 4, 2, 3, 1,…
$ engagement_3 <dbl> 1, 1, 4, 4, 4, 4, 5, 2, 5, 2, 5, 5, 4, 1, 3, 3,…
$ engagement_4_reverse <dbl> 3, 4, 1, 2, 2, 4, 1, 3, 1, 5, 1, 1, 3, 5, 2, 2,…
$ role_clarity_1 <dbl> 1, 1, 4, 4, 5, 4, 3, 2, 3, 3, 5, 3, 2, 1, 1, 2,…
$ role_clarity_2 <dbl> 1, 3, 3, 5, 4, 4, 5, 3, 4, 2, 5, 5, 1, 1, 1, 1,…
$ role_clarity_3 <dbl> 1, 2, 3, 4, 4, 3, 4, 3, 5, 4, 5, 2, 2, 1, 1, 2,…
$ role_clarity_4 <dbl> 1, 4, 3, 3, 3, 2, 5, 5, 5, 3, 4, 4, 2, 1, 1, 3,…
$ workload <dbl> 3.44, 4.68, 4.68, 4.09, 4.26, 2.25, 2.79, 4.16,…
$ organizational_support <dbl> 2.50, 3.92, 5.09, 3.72, 3.63, 3.97, 5.58, 4.25,…
$ job_satisfaction <dbl> 2.52, 4.17, NA, 4.87, 3.17, 5.68, 4.86, 3.77, 5…
$ performance <dbl> 45.1, 43.8, 52.2, 60.3, 59.9, NA, 64.3, 48.1, 6…
$ burnout <dbl> 49.8, 48.5, 47.4, 47.1, 51.8, 36.3, 32.1, 46.6,…
$ turnover_intention <dbl> 4.84, 4.18, 3.66, 4.12, 4.62, 2.83, 3.16, 5.26,…
names(employees) [1] "employee_id" "age" "gender"
[4] "department" "remote_days" "tenure_years"
[7] "manager" "training_group" "leadership_1"
[10] "leadership_2" "leadership_3" "leadership_4_reverse"
[13] "engagement_1" "engagement_2" "engagement_3"
[16] "engagement_4_reverse" "role_clarity_1" "role_clarity_2"
[19] "role_clarity_3" "role_clarity_4" "workload"
[22] "organizational_support" "job_satisfaction" "performance"
[25] "burnout" "turnover_intention"
dim(employees)[1] 480 26
Understand common data types
| Type | Meaning | Example |
|---|---|---|
chr |
text or category | department |
dbl |
numeric value | performance |
int |
whole number | age |
lgl |
logical value |
TRUE or FALSE
|
date |
calendar date | campaign date |
A category may initially appear as text. We can convert it to a factor when that helps the analysis.
Select a variable
employees$performance [1] 45.1 43.8 52.2 60.3 59.9 NA 64.3 48.1 66.0 50.0 58.4 57.0 NA 48.8 53.7
[16] 48.8 44.3 51.1 60.7 64.8 42.9 40.3 45.5 82.4 65.8 40.6 63.7 67.5 69.8 50.0
[31] 31.6 41.5 44.2 67.2 54.2 58.3 49.5 54.6 53.8 75.9 44.7 41.0 56.3 41.6 37.1
[46] 45.1 48.1 53.4 52.7 57.0 43.9 51.2 60.5 59.7 53.6 53.3 59.8 48.8 67.6 38.3
[61] 47.8 46.8 66.2 64.3 60.7 55.4 69.4 43.2 45.3 55.1 66.1 NA 57.1 59.8 57.3
[76] 57.7 52.6 51.1 56.8 52.2 51.2 47.9 53.2 NA 49.9 54.1 60.2 43.9 51.8 59.4
[91] 47.6 64.0 64.7 49.7 56.1 37.8 54.6 43.7 52.3 48.4 53.3 47.2 42.4 53.9 52.9
[106] 51.4 55.2 51.7 60.0 57.0 43.2 52.5 44.5 58.7 56.1 50.4 47.9 55.5 54.3 51.2
[121] 45.6 63.2 52.8 58.7 54.2 48.9 47.2 50.4 54.1 52.3 65.0 51.9 51.8 57.9 54.3
[136] 71.7 64.6 48.7 39.1 49.5 53.6 46.2 69.4 41.6 68.5 52.2 49.2 54.7 64.0 48.3
[151] 76.6 46.2 54.9 51.6 41.8 50.1 39.8 67.5 37.6 58.3 48.9 51.5 52.2 52.9 47.4
[166] 55.3 51.1 58.9 40.7 55.0 59.9 44.4 57.9 72.3 59.3 50.9 56.9 60.4 51.8 43.6
[181] 48.8 56.8 54.0 66.2 59.5 44.3 71.0 68.0 56.9 43.2 63.3 64.1 57.3 40.7 46.5
[196] 45.3 55.9 61.3 50.8 52.0 60.6 58.6 54.5 55.1 58.5 NA 25.0 62.8 NA 58.6
[211] 69.1 67.5 47.6 66.2 45.5 46.8 52.8 50.5 62.1 61.1 45.0 NA 65.2 57.3 37.8
[226] 41.6 61.5 38.9 35.7 42.7 56.8 59.7 56.9 58.6 69.4 52.8 53.9 55.7 57.3 55.6
[241] 63.3 49.4 47.7 49.4 32.7 50.2 36.6 45.6 72.9 48.7 57.2 55.4 51.0 49.5 68.2
[256] 48.5 55.8 52.5 58.3 59.3 55.1 37.2 51.7 56.9 40.5 63.2 47.7 51.5 62.8 53.7
[271] 51.6 54.8 35.6 48.1 59.7 54.7 62.0 40.4 56.9 51.7 67.5 55.5 72.5 49.1 59.4
[286] 45.3 72.8 64.5 56.6 63.2 47.1 55.5 41.7 54.3 44.9 58.0 68.6 49.6 58.0 44.6
[301] 40.4 53.0 37.3 43.8 51.4 65.5 64.8 60.2 45.7 54.8 43.6 47.3 56.6 69.8 37.8
[316] 57.3 52.3 65.3 40.7 63.9 46.1 52.4 57.7 38.2 64.7 56.7 44.9 57.6 56.1 50.3
[331] 53.2 42.1 57.7 61.0 58.5 64.4 48.6 31.7 52.8 59.7 63.3 69.4 56.3 43.3 68.0
[346] 49.3 50.5 54.3 42.7 61.1 42.4 44.0 33.4 56.9 46.7 62.6 69.6 66.4 59.6 44.4
[361] 57.0 59.1 43.0 49.0 36.5 56.3 51.2 58.4 49.6 66.9 52.6 63.1 72.4 57.7 61.0
[376] 39.6 42.3 69.1 60.1 64.7 56.4 59.7 64.7 46.9 47.1 62.3 54.1 57.0 46.6 55.4
[391] 74.5 57.6 49.3 59.3 57.8 56.4 65.0 45.8 59.6 65.9 39.8 52.3 56.2 58.1 61.5
[406] 35.3 63.8 64.5 65.4 49.0 70.9 55.0 81.4 58.8 30.8 47.7 67.8 51.7 54.8 62.0
[421] 55.1 54.2 44.9 72.8 55.0 63.2 55.9 53.7 59.1 40.4 66.6 50.3 58.6 60.8 55.8
[436] 49.4 64.8 57.5 54.7 55.0 65.3 61.1 66.5 60.1 73.8 41.0 46.3 61.5 52.8 60.9
[451] 26.8 54.8 56.6 37.6 52.6 60.4 49.4 51.6 NA 36.5 42.0 43.6 52.7 43.4 57.7
[466] 52.9 41.7 53.4 67.5 64.2 46.0 59.7 34.7 52.1 55.3 41.6 53.6 50.2 46.5 49.2
The $ operator selects one column from a data frame.
Create your own objects
course_name <- "Research Project Design"
number_of_employees <- nrow(employees)
course_name[1] "Research Project Design"
number_of_employees[1] 480
Object names should be descriptive and use underscores rather than spaces.
Getting help
?mean
?read_csvYou can also place the cursor inside a function and press F1 in RStudio.
Common mistake: R is case-sensitive. Performance, performance, and PERFORMANCE are different names.
Practice
- Display the first ten rows with
head(employees, 10). - Find the number of columns using
ncol(employees). - Select the
departmentvariable. - Ask for help about
summary().
Takeaway
An R analysis starts by creating a project, importing data into an object, and checking its structure before calculating anything.
Comments
R ignores text after
#. Comments explain why a step is being performed.