0

I am having trouble subsetting my dataframe in R. I am trying to subset my data using a for loop (or any alternative) where it is separated by the 'TNC_DESCRI'

Table Example

enter image description here

but all I am getting is 'True', 'False'. I am using one of the landcover classes as an example but would like to do this process for all of the landcover classes in the table (thirty-three classes total):

for (Values in TS.df[3]){
for (j in TS.df[3]=='Sedge meadow complex') {
    print(j)
}}

[1] FALSE [1] FALSE [1] FALSE [1] FALSE [1] FALSE [1] FALSE [1] FALSE [1] FALSE

After using dput(head(TS.df[1:10,])) This is what my dataframe looks like:

`structure(list(**OBJECTID** = c(311L, 532L, 1307L, 1439L, 2726L, 
 2960L), **OBJECTID_12_13_14_15_16** = c(145L, 1L, 1L, 1L, 1L, 1L), 
**TNC_DESCRI** = c("Water", "Spruce - fir / feathermoss forest", 
"Spruce - fir / feathermoss forest", "Spruce - fir / feathermoss 
forest", 
"Spruce - fir / feathermoss forest", "Spruce - fir / feathermoss forest"
), **TNC_DESCRI_overlay** = c("Water", "Water", "Spruce - fir / 
feathermoss forest", 
"Spruce - fir / feathermoss forest", "Sedge meadow complex", 
"Spruce - fir / feathermoss forest"), Conversion.1996.2017 = c("Water- 
Water", 
"Spruce - fir / feathermoss forest-Water", "Spruce - fir / feathermoss 
forest-Spruce - fir / feathermoss forest", 
"Spruce - fir / feathermoss forest-Spruce - fir / feathermoss forest", 
"Spruce - fir / feathermoss forest-Sedge meadow complex", 
"Spruce - fir / feathermoss forest-Spruce - fir / feathermoss forest"
), **Change.No.Change** = c("No Change", "Change", "No Change", 
"No Change", "Change", "No Change"), **Edited_overlay** = c("", 
"yes", "", "", "yes", ""), **Edit_Reason_overlay** = c("", "water not 
land", 
"", "", "wetland not upland", ""), **Map_Change_overlay** = c("", 
"map", "", "", "map", ""), **Change_Reason_overlay** = c("", 
"", "", "", "", ""), **Change_Reason_overlay.1** = c("No Change", 
"No Change", "No Change", "No Change", "No Change", "No Change"
), **TCW** = c(NA, -494.21875, -494.21875, -494.21875, -494.21875, 
-494.21875), **TCG** = c(NA, 1599.71875, 1599.71875, 1599.71875, 
1599.71875, 1599.71875), **TCB** = c(NA, 2350.84375, 2350.84375, 
2350.84375, 2350.84375, 2350.84375), **TCW_overlay** = c(NA, 
NA, -341.84, -175.8, NA, -171), **TCG_overlay** = c(NA, NA, 1822.32, 
1167.6, NA, 1600), **TCB_overlay** = c(NA, NA, 2299.68, 1594.6, 
NA, 1939.5), **TCW_Difference** = c(NA, NA, 152.37875, 318.41875, 
NA, 323.21875), **TCG_Difference** = c(NA, NA, 222.60125, -432.11875, 
NA, 0.28125), **TCB_Difference ** = c(NA, NA, -51.1637500000002, 
-756.24375, NA, -411.34375)), **row.names** = c(NA, 6L), class = 
"data.frame")`

What I want to create is a subset for the landcover classes similar to the image below where only the rows with the same class are shown:

Sedge Meadow Complex Example

enter image description here

  • Can you share a reproducible sample of your data by `dput(head(data))`? So we can have a better view of your entire data set at hand. – Anoushiravan R Jun 22 '21 at 22:03
  • I think your are looking for `base::split()` function, this will separate your dataframe into a list. – Dave2e Jun 22 '21 at 23:05
  • Also, `base::by`: `df_list <- by( TS.df, TS.df$TNC_DESC, identity)` – Parfait Jun 22 '21 at 23:13
  • Images are not the right way to share data/code. Add them in a reproducible format which is easier to copy. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). Also see [Split data.frame based on levels of a factor into new data.frames](https://stackoverflow.com/questions/9713294/split-data-frame-based-on-levels-of-a-factor-into-new-data-frames) – Ronak Shah Jun 23 '21 at 02:45
  • @AnoushiravanR Below I am sharing `dput(head(TS.df[1,]))`. The dataset is too big to share anything else – Jay Antonio Oliver Jun 23 '21 at 16:51
  • `tructure(list(OBJECTID = 311L, OBJECTID_12_13_14_15_16 = 145L, TNC_DESCRI = "Water", TNC_DESCRI_overlay = "Water", Conversion.1996.2017 = "Water-Water", Change.No.Change = "No Change", Edited_overlay = "", Edit_Reason_overlay = "", Map_Change_overlay = "", Change_Reason_overlay = "", Change_Reason_overlay.1 = "No Change", TCW = NA_real_, TCG = NA_real_, TCB = NA_real_, TCW_overlay = NA_real_, TCG_overlay = NA_real_, TCB_overlay = NA_real_, TCW_Difference = NA_real_, TCG_Difference = NA_real_, TCB_Difference = NA_real_), row.names = 1L, class = "data.fram")` – Jay Antonio Oliver Jun 23 '21 at 16:51
  • @RonakShah Thank you so much for that. I am new to this so its always useful to know how to properly share for next time. Also, the split data.frame worked, it is exactly what I needed. – Jay Antonio Oliver Jun 23 '21 at 19:43

1 Answers1

0

I'm not sure if I understood the question correctly... you could first get the list of landcover classes by

landcover_class = unique(TS.df$TNC_DESCRI)

then use it in a for loop

for(i in landcover_class){
    TS.df[TS.df$TNC_DESCRI==i,]
}

Then you would get subset dataframes for each landcover classes

sammy
  • 427
  • 1
  • 3
  • 14