0

I have a large data set in a df, included is a sample of the df.

I am trying to figure out the best way to take all the rows of df from every 5 rows and store them in a new df. So wavelength 350, wavelength 355, etc.. and all the other columns and corresponding rows that are in the df included in that 5 step interval. Wavelength is the main field to use for the intervals.

structure(list(wavelength = 350:360, albedo = c(0.88046, 0.88089, 
0.88125, 0.88135, 0.88156, 0.88184, 0.88217, 0.88224, 0.88208, 
0.88211, 0.88231), date = structure(c(17970, 17970, 17970, 17970, 
17970, 17970, 17970, 17970, 17970, 17970, 17970), class = "Date"), 
    location2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L), .Label = "gc1", class = "factor"), albedo_corr = c(0.900254852108277, 
    0.900659453953826, 0.900988637085391, 0.90103193598839, 0.90126832240164, 
    0.901613616766539, 0.901975071632926, 0.902042155285601, 
    0.901846492887525, 0.90185458010468, 0.902082926871999), 
    cat2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L), .Label = "Open", class = "factor")), row.names = c(NA, 
11L), class = "data.frame")
Alex
  • 57
  • 6
  • 1
    Try `df[seq(1,nrow(df),5),]` – Martin Gal May 21 '20 at 22:22
  • @Martin Gal Ok I started with that, it works for the first observation (wavelengths range from 350-1900, than the next observation of wavelengths begins again at 350 to 1900), however the next observation in the df then starts at wavelength 354, 359, 364. – Alex May 21 '20 at 22:27
  • 1
    So you have multiple wavelengths 350 that you all want to select, i.e. you want to get all 350, all 355 etc? – Martin Gal May 21 '20 at 22:30
  • Exactly. Like say obs = 1 goes wavlength 350-1900, than the next obs = 2 starts and goes again 350-1900, they are all stacked on each other in the df – Alex May 21 '20 at 22:32
  • So basically you don't want every 5th row, but every wavelength with wavelength mod 5 = 0? – Martin Gal May 21 '20 at 22:33
  • 2
    In this case: Try `df %>% filter(wavelength %% 5 == 0)`. – Martin Gal May 21 '20 at 22:34
  • 1
    That did it thanks buddy! – Alex May 21 '20 at 22:36

0 Answers0