重新排列R中的数据

时间:2017-07-12 19:25:00

标签: r dataframe dplyr

我有一个像这样排列的大型数据集

Stat.num   LatS.dec.NEW LonS.dec.NEW    LatF.dec.NEW   LonF.dec.NEW 
388        66.68         -21.0666      66.7071666       -20.98  
389        66.69         -21.01        66.6433         -21.06

但我想像这样重新安排

Stat.numb order  Lat              Lon
388       1     66.68           -21.06666
388       2     66.7071         -20.98  
389       1     66.6            -21.01  
389       2     66.643          -21.06  

我一直试图用dplyr来解决这个问题,但我还没有找到解决方案并感谢你的帮助。

提前致谢

2 个答案:

答案 0 :(得分:2)

要使用tidyverse从长到大格式重新排列数据集,您可以使用tidyr包含函数spreadgather

# just for a reproductible df

df <-
  structure(
    list(
      Stat.num = 388:389,
      LatS.dec.NEW = c(66.68, 66.69),
      LonS.dec.NEW = c(-21.0666,-21.01),
      LatF.dec.NEW = c(66.7071666,
                       66.6433),
      LonF.dec.NEW = c(-20.98,-21.06)
    ),
    .Names = c(
      "Stat.num",
      "LatS.dec.NEW",
      "LonS.dec.NEW",
      "LatF.dec.NEW",
      "LonF.dec.NEW"
    ),
    row.names = c(NA,-2L),
    class = "data.frame"
  )

df
#>   Stat.num LatS.dec.NEW LonS.dec.NEW LatF.dec.NEW LonF.dec.NEW
#> 1      388        66.68     -21.0666     66.70717       -20.98
#> 2      389        66.69     -21.0100     66.64330       -21.06

library(tidyr)
library(dplyr)
#> 
#> Attachement du package : 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
df %>%
  gather("type", "value", -Stat.num) %>%
  separate(type, c("type", "order", "drop"), sep = c(3, 4)) %>%
  select(-drop) %>%
  spread(type, value)
#>   Stat.num order      Lat      Lon
#> 1      388     F 66.70717 -20.9800
#> 2      388     S 66.68000 -21.0666
#> 3      389     F 66.64330 -21.0600
#> 4      389     S 66.69000 -21.0100

答案 1 :(得分:2)

一个班轮:

> data
  Stat.num LatS.dec.NEW LonS.dec.NEW LatF.dec.NEW LonF.dec.NEW
1      388        66.68     -21.0666     66.70717       -20.98
2      389        66.69     -21.0100     66.64330       -21.06

并做:

> nr = nrow(data)
> setNames(data.frame(rep(data$Stat.num,rep(2,nr)),rep(1:2,nr),matrix(t(data[,-1]),ncol=2,byrow=TRUE)),c("Stat.num","order","Lat","Long"))
  Stat.num order      Lat     Long
1      388     1 66.68000 -21.0666
2      388     2 66.70717 -20.9800
3      389     1 66.69000 -21.0100
4      389     2 66.64330 -21.0600

这是否对您的&#34;大&#34;数据取决于它的大小。

它的工作原理是将最后四列重新排列成两列矩阵,然后通过各种重复和序列为新数据帧构建适当的向量。