-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path33 Pivot_longer_wider_DPLYR.R
More file actions
59 lines (45 loc) · 1.22 KB
/
Copy path33 Pivot_longer_wider_DPLYR.R
File metadata and controls
59 lines (45 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# PIVOT
library(tidyverse)
# Create initial data set
Country <-c("Spain","France","India","Germany")
Views <-c(100,2261,6725,800)
data_views_a <-cbind.data.frame(Country,Views)
data_views_a
Country Views
1 Spain 100
2 France 2261
3 India 6725
4 Germany 800
# 1. Pivot wider (tidyr, dplyr)
# pivot_wider(names_from = var1, values_from = var2)
data_wide <- data_views_a %>%
pivot_wider(names_from = Country,
values_from =Views)
data_wide
> data_wide
# A tibble: 1 x 4
Spain France India Germany
<dbl> <dbl> <dbl> <dbl>
1 100 2261 6725 800
# 2. Pivot longer (tidyr, dplyr)
# pivot_longer(cols = 1:n, names_to = var1, values_to = var2)
Spain<-c(100)
France<-c(2261)
India<-c(6725)
data_views_b <-cbind.data.frame(Spain,France,India)
data_views_b
> data_views_b
Spain France India
1 100 2261 6725
data_long <- data_views_b %>%
pivot_longer( cols = 1:3,
names_to = "country",
values_to = "views")
data_long
> data_long
# A tibble: 3 x 2
country views
<chr> <dbl>
1 Spain 100
2 France 2261
3 India 6725