简体   繁体   中英

Rearranging data in R

I have a large dataset that is arranged like this

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

But I would like to re-arrange like this

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  

I have been trying to solve this with dplyr, but I have not yet found the solution and would appreciate your help.

With thanks in advance

To rearrange dataset from long to wide format using tidyverse , you can use tidyr package with function spread and gather

# 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

One liner:

> 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

And do:

> 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

Whether or not this is efficient for your "large" data depends on how large it is.

It works by rearranging the last four columns into a two column matrix and then constructing appropriate vectors for the new data frame by various repeats and sequences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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