Home > Back-end >  Filtering the whole first two columns in dplyr
Filtering the whole first two columns in dplyr

Time:11-16

I have multiple time series data and I want to apply Jag code and how can I use the whole first two columns in filtering because the combination of the first two columns is a unique combination that I want to use that for JAG code?

    Site    SP   Year   Abun    
     A3   BG    1957    81  
     A3   BG    1958    45  
     A3   BG    1959    96  
     A3   BG    1960    44  
     A3  BG    1961     24  
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
…………………………..
  A3    BD  1957    135 
  A3    BD  1958    91  
  A3    BD  1959    171 
  A3    BD  1960    88  

I tried for only one column as follows:

for (SP in c("BG”  “BD", "CD", "DC", "AC", "DB")){
y <- data %>% 
    filter(SP == sp, year > 1956, year < 1998) %>% 
    select(Abun) 

}

I want to filter or uniquely separate the data taking the whole combination of the first two columns (Site and SP), any help would be appreciated?

CodePudding user response:

If we want to separate as multiple datasets, use split to return a list of datasets

data1 <- subset(data, SP %in% c("BG”  “BD", "CD", "DC", "AC", "DB") & 
                year > 1956 & year < 1998))
lst1 <- split(data1, data1[c("Site", "SP")], drop = TRUE)
  •  Tags:  
  • r
  • Related