Search code examples
rdataframedplyrtidyrdata-cleaning

How to create an empty named specified column and position this column into a particular numbered position within the data frame in R


I have a data frame with 388 columns, and I have two columns in my data frame labelled as 'Longitude_E' and 'Latitude_N', containing GPS coordinates in degrees, minutes, and seconds.

I aim to create two empty columns, and, label them as 'Decimal_Degrees_Longitude_E' and 'Degrees_Decimal_Latitude_N' because I want to add GPS coordinates in decimal degrees.

I know how to create two empty columns with specified names, but I can't figure out how to insert them within my data frame in a particular position using packages such as Dplyr or Tidyr.

#Create two columns with decimal degrees coordinates 

Df[,"Decimal_Degrees_Longitude_E"] <- NA
Df[,"Decimal_Degrees_Latitude_N"] <- NA

The columns containing GPS positions in degrees, minutes and seconds are column numbers 12 and 13.

I'd like the two new empty columns labelled as 'Decimal_Degrees_Longitude_E' and 'Degrees_Decimal_Latitude_N' to be positioned as column numbers 14 and 15.


Solution

  • When using dplyr::mutate to create your columns you can specify the position where the columns should be inserted via .after= or .before:

    Df <- matrix(
      rep(1:20, 2),
      ncol = 20
    ) |>
      data.frame()
    
    library(dplyr, warn = FALSE)
    
    Df |>
      mutate(
        Decimal_Degrees_Longitude_E = NA,
        Decimal_Degrees_Latitude_N = NA,
        .after = 13
      )
    #>   X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 Decimal_Degrees_Longitude_E
    #> 1  1  3  5  7  9 11 13 15 17  19   1   3   5                          NA
    #> 2  2  4  6  8 10 12 14 16 18  20   2   4   6                          NA
    #>   Decimal_Degrees_Latitude_N X14 X15 X16 X17 X18 X19 X20
    #> 1                         NA   7   9  11  13  15  17  19
    #> 2                         NA   8  10  12  14  16  18  20
    

    And to relocate columns after they have been created you could use dplyr::relocate:

    Df[,"Decimal_Degrees_Longitude_E"] <- NA
    Df[,"Decimal_Degrees_Latitude_N"] <- NA
    
    Df |>
      relocate(
        Decimal_Degrees_Longitude_E,
        Decimal_Degrees_Latitude_N,
        .after = 13
      )
    #>   X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 Decimal_Degrees_Longitude_E
    #> 1  1  3  5  7  9 11 13 15 17  19   1   3   5                          NA
    #> 2  2  4  6  8 10 12 14 16 18  20   2   4   6                          NA
    #>   Decimal_Degrees_Latitude_N X14 X15 X16 X17 X18 X19 X20
    #> 1                         NA   7   9  11  13  15  17  19
    #> 2                         NA   8  10  12  14  16  18  20