[英]Updating dataframe in R for each observation where condition occurs transform to 2 rows
[英]"Transform" multiple rows for each variable observation into single columns in R
假設我有一個看起來像這樣的數據框(實際上,它是一個更大的數據框,包含數百個變量和數千個觀察值)。
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
我需要將每個變量的多行“轉換”為列並匹配相應的值,所以它看起來像這樣:
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
同樣,在實踐中假設有更多的變量和觀察結果。 任何人都知道一個簡單的代碼片段來使這變得容易? 謝謝。
您可以使用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
這將是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"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.