Load base packages manually
library(datasets) # For example datasets
Install pacman ("package manager") if needed
if (!require("pacman")) install.packages("pacman")
pacman must already be installed; then load contributed
packages (including pacman) with pacman
pacman::p_load(magrittr, pacman, rio, tidyverse)
pacman: for loading/unloading packages
magrittr: for pipes
rio: for importing data
tidyverse: for so many reasons
This script makes use of functions from the forcats
package, which is installed as part of the tidyverse
LOAD AND PREPARE DATA
Import data into tibble "df"
df <- import("Documents/Job_Stuff/Active_LinkedIn_Learning_Courses_Files/Ex_Files_R_Visualizing_Data/Exercise_Files/data/MobileOS_US.xlsx") %>%
- as_tibble() %>%
- print()
A tibble: 18 × 126
MobileOS 2009-…¹ 2009-…² 2009-…³ 2009-…⁴ 2009-…⁵ 2009-…⁶ 2009-…⁷
1 Android 1.75 5.47 6.12 5.56 5.47 0.3 5.19
2 BlackBerry OS 2.16 12.4 4.74 5.52 13.0 20.1 20.1
3 Brew 0 0 0 0 0 0 0
4 iOS 59.2 60.5 68.1 69.8 63.1 60.0 56.6
5 LG 0 0 0 0 0 0 0.14
6 Linux 3.33 0 0 0 0 0 0
7 Nintendo 0 0 0 0 0 0 0.17
8 Nintendo 3DS 0 0 0 0 0 0 0
9 Nokia Unknown 0 0 0 0 0 0 0
10 Other 1.61 1.85 1.74 1.65 1.56 1.45 1.34
11 Playstation 7.51 6.39 6.35 5.4 5.02 5.04 4.43
12 Samsung 0 0 0 0 0 0 0.58
13 Series 40 0 0 0 0 0 0 0
14 Sony Ericsson 0 0 0 0 0 0 0.11
15 SymbianOS 5.61 3.05 2.74 2.47 2.64 2.95 1.1
16 Unknown 14.3 5.9 6.48 6.35 6.36 7.37 7.38
17 webOS 0 0 0 0 0 0 0.22
18 Windows 4.58 4.5 3.77 3.25 2.87 2.84 2.56
… with 118 more variables: 2009-08
, 2009-09
,
2009-10
, 2009-11
, 2009-12
, 2010-01
,
2010-02
, 2010-03
, 2010-04
, 2010-05
,
2010-06
, 2010-07
, 2010-08
, 2010-09
,
2010-10
, 2010-11
, 2010-12
, 2011-01
,
2011-02
, 2011-03
, 2011-04
, 2011-05
,
2011-06
, 2011-07
, 2011-08
, …
Use colnames()
to see all variable names
Define "MobileOS" as factor
df %<>%
- mutate(MobileOS = as.factor(MobileOS)) %>%
- print()
A tibble: 18 × 126
MobileOS 2009-…¹ 2009-…² 2009-…³ 2009-…⁴ 2009-…⁵ 2009-…⁶ 2009-…⁷
1 Android 1.75 5.47 6.12 5.56 5.47 0.3 5.19
2 BlackBerry OS 2.16 12.4 4.74 5.52 13.0 20.1 20.1
3 Brew 0 0 0 0 0 0 0
4 iOS 59.2 60.5 68.1 69.8 63.1 60.0 56.6
5 LG 0 0 0 0 0 0 0.14
6 Linux 3.33 0 0 0 0 0 0
7 Nintendo 0 0 0 0 0 0 0.17
8 Nintendo 3DS 0 0 0 0 0 0 0
9 Nokia Unknown 0 0 0 0 0 0 0
10 Other 1.61 1.85 1.74 1.65 1.56 1.45 1.34
11 Playstation 7.51 6.39 6.35 5.4 5.02 5.04 4.43
12 Samsung 0 0 0 0 0 0 0.58
13 Series 40 0 0 0 0 0 0 0
14 Sony Ericsson 0 0 0 0 0 0 0.11
15 SymbianOS 5.61 3.05 2.74 2.47 2.64 2.95 1.1
16 Unknown 14.3 5.9 6.48 6.35 6.36 7.37 7.38
17 webOS 0 0 0 0 0 0 0.22
18 Windows 4.58 4.5 3.77 3.25 2.87 2.84 2.56
… with 118 more variables: 2009-08
, 2009-09
,
2009-10
, 2009-11
, 2009-12
, 2010-01
,
2010-02
, 2010-03
, 2010-04
, 2010-05
,
2010-06
, 2010-07
, 2010-08
, 2010-09
,
2010-10
, 2010-11
, 2010-12
, 2011-01
,
2011-02
, 2011-03
, 2011-04
, 2011-05
,
2011-06
, 2011-07
, 2011-08
, …
Use colnames()
to see all variable names
Select 2010 variable, convert to whole numbers
df %<>%
- mutate(OS_2010 =
2010-01
* 100) %>% - select(MobileOS, OS_2010) %>%
- print()
A tibble: 18 × 2
MobileOS OS_2010
1 Android 1190
2 BlackBerry OS 2013
3 Brew 0
4 iOS 5358
5 LG 52
6 Linux 0
7 Nintendo 79
8 Nintendo 3DS 0
9 Nokia Unknown 0
10 Other 77
11 Playstation 218
12 Samsung 174
13 Series 40 0
14 Sony Ericsson 43
15 SymbianOS 141
16 Unknown 433
17 webOS 117
18 Windows 105
Check for outliers
df %>%
- select(OS_2010) %>%
- boxplot(horizontal = T)
Convert to rows
df %<>%
- uncount(OS_2010) %>%
- print()
Error inuncount()
:
! Can't convert fromweights
to due to loss of precision.
• Locations: 11
Runrlang::last_error()
to see where the error occurred.
rlang::last_error()
<error/vctrs_error_cast_lossy>
Error inuncount()
:
! Can't convert fromweights
to due to loss of precision.
• Locations: 11
Backtrace:
- df %<>% uncount(OS_2010) %>% print()
- tidyr::uncount(., OS_2010)
Runrlang::last_trace()
to see the full context.
rlang::last_trace()
<error/vctrs_error_cast_lossy>
Error inuncount()
:
! Can't convert fromweights
to due to loss of precision.
• Locations: 11
Backtrace:
▆
- ├─df %<>% uncount(OS_2010) %>% print()
- ├─base::print(.)
- └─tidyr::uncount(., OS_2010)
- └─vctrs::vec_cast(w, integer(), x_arg = "weights")
-
└─vctrs (local) `<fn>`()
-
└─vctrs:::vec_cast.integer.double(...)
-
└─vctrs::maybe_lossy_cast(...)
-
├─base::withRestarts(...)
-
│ └─base (local) withOneRestart(expr, restarts[[1L]])
-
│ └─base (local) doWithOneRestart(return(expr), restart)
-
└─vctrs:::stop_lossy_cast(...)
-
└─vctrs::stop_incompatible_cast(...)
-
└─vctrs::stop_incompatible_type(...)
-
└─vctrs:::stop_incompatible(...)
-
└─vctrs:::stop_vctrs(...)
-
└─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = vctrs_error_call(call))