Let's say I have a dataframe that looks like this (in practice, it is a much larger dataframe with hundreds of variables and thousands of observation values).
VAR Country Year Value
1 VAR 1 France 2014 1
2 VAR 1 France 2018 2
3 VAR 1 UK 2014 5
4 VAR 1 UK 2018 6
5 VAR 2 France 2014 2
6 VAR 2 France 2018 3
7 VAR 2 UK 2014 7
8 VAR 2 UK 2018 8
I need to "transform" the multiple rows for each variable into columns and match the corresponding values, so it looks like this:
Country Year VAR 1 VAR 2
1 France 2014 1 2
2 France 2018 2 3
3 UK 2014 5 7
4 UK 2018 6 8
Again, assume in practice that there are many more variables and observations. Anyone know a simple snippet of code to make this easy? Thanks.
You could use a data.table
dcast
:
library(data.table)
rm(list = ls())
dt <- data.table(
VAR = c('VAR 1', 'VAR 1', 'VAR 1','VAR 1', 'VAR 2', 'VAR 2', 'VAR 2', 'VAR 2'),
Country = c('France', 'France', 'UK', 'UK', 'France', 'France', 'UK', 'UK'),
Year = c(2014, 2018, 2014, 2018, 2014, 2018, 2014, 2018),
Value = c(1, 2, 5, 6, 2, 3, 7, 8)
)
dt.wide <- dcast(
dt,
Country + Year ~ VAR, value.var = 'Value'
)
dt.wide
> dt.wide
Country Year VAR 1 VAR 2
1: France 2014 1 2
2: France 2018 2 3
3: UK 2014 5 7
4: UK 2018 6 8
It would be an option with pivot_wider
library(dplyr)
library(tidyr)
df1 %>%
pivot_wider(names_from = VAR, values_from = Value)
# A tibble: 4 x 4
# Country Year `VAR 1` `VAR 2`
# <chr> <int> <int> <int>
#1 France 2014 1 2
#2 France 2018 2 3
#3 UK 2014 5 7
#4 UK 2018 6 8
df1 <- structure(list(VAR = c("VAR 1", "VAR 1", "VAR 1", "VAR 1", "VAR 2",
"VAR 2", "VAR 2", "VAR 2"), Country = c("France", "France", "UK",
"UK", "France", "France", "UK", "UK"), Year = c(2014L, 2018L,
2014L, 2018L, 2014L, 2018L, 2014L, 2018L), Value = c(1L, 2L,
5L, 6L, 2L, 3L, 7L, 8L)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.