0

Assume I have a big data frame with 'Sample_n', 'Station', 'Lat' and 'Lon'.

Each station (from "1" to "100", for example) has a different amount of samples (random amount from 1 to 99, for example).

Although there are no gaps in 'Sample_n' and 'Station', 'Lat' and 'Lon' are full of NAs, but there is at least a full coordinate (lat+lon) for each station, placed at a random row.

I would like to fill the gaps of 'Lat' and 'Lon', associating the right values to the right station.

Strobila
  • 317
  • 3
  • 15

1 Answers1

1

A sample of your data would be of help. My best guess is that you can get what you want using the function fill from tidyr (also using functions in dplyr in the example):

library(tidyr)
library(dplyr)

df <- tibble(Sample_n=rep(1:3, each = 3), Station = rep(letters[1:3], each = 3),
             lat = c(NA, 50, NA, 40, NA, NA, NA, 55, NA),
             lon = c(NA, 150, NA, 140, NA, NA, NA, 155, NA))

df

# A tibble: 9 x 4
  Sample_n Station   lat   lon
     <int> <chr>   <dbl> <dbl>
1        1 a          NA    NA
2        1 a          50   150
3        1 a          NA    NA
4        2 b          40   140
5        2 b          NA    NA
6        2 b          NA    NA
7        3 c          NA    NA
8        3 c          55   155
9        3 c          NA    NA

df %>% group_by(Sample_n, Station) %>% 
  fill(lat, lon, .direction="updown")

# A tibble: 9 x 4
# Groups:   Sample_n, Station [3]
  Sample_n Station   lat   lon
     <int> <chr>   <dbl> <dbl>
1        1 a          50   150
2        1 a          50   150
3        1 a          50   150
4        2 b          40   140
5        2 b          40   140
6        2 b          40   140
7        3 c          55   155
8        3 c          55   155
9        3 c          55   155
Claudio
  • 1,229
  • 6
  • 11