简体   繁体   中英

I am attempting to recall how to subset from a dataframe selecting specific rows while keeping the Column names in R

Columns: Area, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019

Rows: Woodbury County, Iowa | Plymouth County, Iowa | Webster County, Iowa | Black Hawk County, Iowa | Polk County, Iowa | Poweshiek County, Iowa | Linn County, Iowa | Dubuque County, Iowa | Scott County, Iowa | Rock Island County, Iowa

I don't necessarily need a completed answer for my question, a possible syntax would be sufficient. Any tips or advice on how to make this Process easier would be appreciated as well.

Thank you

Edit: I also need the data that is associated with the column/row

You subset a dataframe by row name using the following format where df is a dataframe containing data, df[row, col] where row and col can be an index or a name (if named). You can subset by names using a vector of the names you want. You didn't really give an example, however the example below demonstrates this.

df <- data.frame(matrix(1, 10, 11))
rms <- c("Woodbury County, Iowa", "Plymouth County, Iowa", "Webster County, Iowa", 
     "Black Hawk County, Iowa", "Polk County, Iowa", "Poweshiek County, Iowa", 
     "Linn County, Iowa", "Dubuque County, Iowa", "Scott County, Iowa", 
     "Rock Island County, Iowa")
colnames(df) <- c('Area', 2010:2019)
rownames(df) <- rms
print(df)
df[c("Woodbury County, Iowa", "Plymouth County, Iowa", "Webster County, Iowa"), ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM