我有以下虚拟数据:
library(dplyr)
library(tidyr)
library(reshape2)
dt <- expand.grid(Year = 1990:2014, Product=LETTERS[1:8], Country = paste0(LETTERS, "I")) %>% select(Product, Country, Year)
dt$value <- rnorm(nrow(dt))
Run Code Online (Sandbox Code Playgroud)
我选择了两个产品国家组合
sdt <- dt %>% filter((Product == "A" & Country == "AI") | (Product == "B" & Country =="EI"))
Run Code Online (Sandbox Code Playgroud)
我希望每个组合并排看到这些值.我可以这样做dcast:
sdt %>% dcast(Year ~ Product + Country)
Run Code Online (Sandbox Code Playgroud)
是否有可能spread从包tidyr做到这一点?
akr*_*run 59
一种选择是通过加入'Product'和'Country'列来创建一个新的'Prod_Count' paste,删除那些列,select并使用spreadfrom 从'long'重新整形为'wide' tidyr.
library(dplyr)
library(tidyr)
sdt %>%
mutate(Prod_Count=paste(Product, Country, sep="_")) %>%
select(-Product, -Country)%>%
spread(Prod_Count, value)%>%
head(2)
# Year A_AI B_EI
#1 1990 0.7878674 0.2486044
#2 1991 0.2343285 -1.1694878
Run Code Online (Sandbox Code Playgroud)
或者我们可以通过使用unitefrom tidyr(来自@ beetroot的评论)避免一些步骤,并像以前一样重塑形状.
sdt%>%
unite(Prod_Count, Product,Country) %>%
spread(Prod_Count, value)%>%
head(2)
# Year A_AI B_EI
# 1 1990 0.7878674 0.2486044
# 2 1991 0.2343285 -1.1694878
Run Code Online (Sandbox Code Playgroud)
使用pivot_wider()tidyr 1.0.0版中引入的新功能时,可以通过一个函数调用来实现。
pivot_wider()(counterpart pivot_longer():)与相似spread()。但是,它提供了其他功能,例如使用多个键/名称列(和/或多个值列)。为此,自变量(names_from指示从哪个列中获取新变量的名称)可以采用多个列名称(此处Product和Country)。
library("tidyr")
sdt %>%
pivot_wider(id_cols = Year,
names_from = c(Product, Country)) %>%
head(2)
#> # A tibble: 2 x 3
#> Year A_AI B_EI
#> <int> <dbl> <dbl>
#> 1 1990 -2.08 -0.113
#> 2 1991 -1.02 -0.0546
Run Code Online (Sandbox Code Playgroud)
另请参阅:https : //tidyr.tidyverse.org/articles/pivot.html
| 归档时间: |
|
| 查看次数: |
28262 次 |
| 最近记录: |