繁体   English   中英

在R中重新排列数据

[英]Rearranging data in R

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

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解决此问题,但是我还没有找到解决方案,非常感谢您的帮助。

预先感谢

要使用从长重新排列数据集宽幅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

一班轮:

> 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

这对于您的“大”数据是否有效取决于其大小。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM