0

I have four tables that are very much similar. I would like to combine the tables according to a filter that is set for each table according to declaration periods (DECL_PERIO)

I made sure that each of the tables have the same column names as follows:

colnames(Terr_data_HM90) <- c("OID", "ObjectID", "Value", "Count", "PA2021Q4_2", 
                              "NVM2018_v2", "HM70LC90_20L1", "Cur_Name_1", 
                              "DECL_PERIO", "DECL_YEAR", "LC90_20L1", "Count_NVM",
                              "Name_18", "CNSRV_TRGT", "ConTM")
colnames(Terr_data_HM14) <- c("OID", "ObjectID", "Value", "Count", "PA2021Q4_2", 
                              "NVM2018_v2", "HM70LC90_20L1", "Cur_Name_1", 
                              "DECL_PERIO", "DECL_YEAR", "LC90_20L1", "Count_NVM",
                              "Name_18", "CNSRV_TRGT", "ConTM")
colnames(Terr_data_HM18) <- c("OID", "ObjectID", "Value", "Count", "PA2021Q4_2", 
                              "NVM2018_v2", "HM70LC90_20L1", "Cur_Name_1", 
                              "DECL_PERIO", "DECL_YEAR", "LC90_20L1", "Count_NVM",
                              "Name_18", "CNSRV_TRGT", "ConTM")
colnames(Terr_data_HM20) <- c("OID", "ObjectID", "Value", "Count", "PA2021Q4_2", 
                              "NVM2018_v2", "HM70LC90_20L1", "Cur_Name_1", 
                              "DECL_PERIO", "DECL_YEAR", "LC90_20L1", "Count_NVM",
                              "Name_18", "CNSRV_TRGT", "ConTM")

My code to create the combined table according to the filters as follows:

 Terra_data <- as.data.frame((Terr_data_HM90 %>% filter(DECL_PERIO == "1901-1950"| DECL_PERIO =="1951-2000"| DECL_PERIO =="2001-2010"|DECL_PERIO =="2011-2015" )), 
                            (Terr_data_HM14 %>% filter(DECL_PERIO == "2016"| DECL_PERIO =="2017")),
                            (Terr_data_HM18 %>% filter(DECL_PERIO == "2018"| DECL_PERIO =="2019")),
                            (Terr_data_HM20 %>% filter(DECL_PERIO == "2020"| DECL_PERIO =="2021")))

The code worked but only for the first filter and does not include the filters for Terr_data_HM14 to HM20. I am not sure what I am doing incorrectly.

I can use the tables separately in a calculation and at the end join my final results according to the specified filters in the code above. But I thought it would be better to create one table so that I can run the calculation code once and not four times.

Shae11
  • 49
  • 5
  • Can you provide a reproducible example? https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Andrea M Jun 24 '22 at 13:38
  • 2
    What do you mean by "join"? Do you want to stack? Then use `cbind()` or `bind_cols()` instead of `as.data.frame()`. –  Jun 24 '22 at 13:40
  • I have four tables they all have the same data but their values differ according to the HM (HM90, HM14, HM18, HM20) each table has declaration periods from 1901-2021. I want to extract only the specific declarations from each table so for HM90 I want all data from 1901 to 2015, for HM14 I want all data for 2016 and 2017. I wont be adding columns I will be adding rows so if HM90 for the years ranging from 1901 to 2015 has 45 rows and HM14 for the years 2016 and 2017 has 20 rows. My final table will have 65 rows. Hope this comment makes sense. – Shae11 Jun 24 '22 at 14:11
  • 1
    @Shae11 then I misspoke. Does `bind_rows()` do what you want, instead of `as.data.frame()`? –  Jun 24 '22 at 17:44

0 Answers0