1

我有以下数据框(此处显示的头部),它很乱。

orig     dest   1997    2002      2006   2010     2016    1997    2002      2006   2010     2016 
Seoul   Inchon   543    524       364     452       845     543    524         364    452     845 
Seoul   Gyeongi  543    524       364     452       845     543    524         364    452     845
Inchon  Seoul    543    524       364     452       845     543    524         364    452     845

我想将我的数据集转换为以下格式(它有 7000 个观察值,我刚刚展示了数据集的头部):我想得到(1997-2016 在名为“cartrip”的一列下)和(1997-2016 在另一列名为“行走”)

Year orig  dest      cartrip  walking 
1997 Seoul Incheon   543      543 
2002 Seoul Incheon   524      524  
2006 Seoul Incheon   364      364
2010 Seoul Incheon   452      452
2016 Seoul Incheon   845      845
1997 Seoul Gyeongi   543      543 
2002 Seoul Gyeongi   524      524  
2006 Seoul Gyeongi   364      364
2010 Seoul Gyeongi   452      452
2016 Seoul Gyeongi   845      845

我试图收集,但没有用。

4

2 回答 2

1

您不应该在数据框中有重复的列名,我们使用make.unique.

names(df) <- make.unique(names(df))

然后我们可以删除空行并使用 pivot_longer.

library(dplyr)
library(tidyr)

df %>%
  filter(orig != '' | dest != '') %>%
  pivot_longer(cols = -c(orig, dest), 
               names_to = c('.value', 'index'), 
               names_sep = '\\.') %>%
  select(-index)

对于更新的数据集,我们可以使用:

df %>%
  pivot_longer(cols = -c(orig, dest), names_to = 'year') %>%
  mutate(.copy = c('cartrip', 'walking')[.copy]) %>%
  pivot_wider(names_from = .copy, values_from = value)

#   orig   dest    year  cartrip walking
#   <fct>  <fct>   <chr>   <int>   <int>
# 1 Seoul  Inchon  1997      543     543
# 2 Seoul  Inchon  2002      524     524
# 3 Seoul  Inchon  2006      364     364
# 4 Seoul  Inchon  2010      452     452
# 5 Seoul  Inchon  2016      845     845
# 6 Seoul  Gyeongi 1997      543     543
# 7 Seoul  Gyeongi 2002      524     524
# 8 Seoul  Gyeongi 2006      364     364
# 9 Seoul  Gyeongi 2010      452     452
#10 Seoul  Gyeongi 2016      845     845
#11 Inchon Seoul   1997      543     543
#12 Inchon Seoul   2002      524     524
#13 Inchon Seoul   2006      364     364
#14 Inchon Seoul   2010      452     452
#15 Inchon Seoul   2016      845     845
于 2020-04-23T06:54:51.200 回答
0

一个data.table解决方案。您可能需要在year. melt现在data.table无法正确处理您year的问题。我想pivot_longertidyr可以一口气做到这一点。

library(data.table)

df <- fread('orig   dest    cartrip cartrip cartrip cartrip cartrip walking walking walking walking walking
        1997    2002    2006    2010    2016    1997    2002    2006    2010    2016
Seoul   Inchon  543 524 364 452 845 543 524 364 452 845
Seoul   Gyeongi 543 524 364 452 845 543 524 364 452 845
Inchon  Seoul   543 524 364 452 845 543 524 364 452 845
')


result <- melt(df[orig!="",],measure.vars = patterns(walking="^walking",cartrip="^cartrip"),variable.name = "year")

result[,year:=forcats::lvls_revalue(year,c("1997", "2002", "2006", "2010", "2016")
)]

result[order(orig,dest)][,.(year,orig,dest,cartrip,walking)]
#>     year   orig    dest cartrip walking
#>  1: 1997 Inchon   Seoul     543     543
#>  2: 2002 Inchon   Seoul     524     524
#>  3: 2006 Inchon   Seoul     364     364
#>  4: 2010 Inchon   Seoul     452     452
#>  5: 2016 Inchon   Seoul     845     845
#>  6: 1997  Seoul Gyeongi     543     543
#>  7: 2002  Seoul Gyeongi     524     524
#>  8: 2006  Seoul Gyeongi     364     364
#>  9: 2010  Seoul Gyeongi     452     452
#> 10: 2016  Seoul Gyeongi     845     845
#> 11: 1997  Seoul  Inchon     543     543
#> 12: 2002  Seoul  Inchon     524     524
#> 13: 2006  Seoul  Inchon     364     364
#> 14: 2010  Seoul  Inchon     452     452
#> 15: 2016  Seoul  Inchon     845     845

reprex 包(v0.3.0)于 2020 年 4 月 23 日创建

于 2020-04-23T07:14:29.033 回答