[r] How to select some rows with specific rownames from a dataframe?

I have a data frame with several rows. I want to select some rows with specific rownames (such as stu2,stu3,stu5,stu9) from this dataframe. The input example dataframe is as follows:

        attr1 attr2 attr3 attr4
  stu1      0     0     1     0
  stu2     -1     1    -1     1
  stu3      1    -1     0    -1
  stu4      1    -1     1    -1
  stu5     -1     1     0     1
  stu6      1    -1     1     0
  stu7     -1    -1    -1     1
  stu8      1    -1     0    -1
  stu9     -1    -1     1    -1
  stu10    -1     1     0     1

Expected output:

        attr1 attr2 attr3 attr4
  stu2     -1     1    -1     1
  stu3      1    -1     0    -1
  stu5     -1     1     0     1
  stu9     -1    -1     1    -1

This question is related to r dataframe subset rowname

The answer is


df <- data.frame(x=rnorm(10), y=rnorm(10))
rownames(df) <-  letters[1:10]
df[c('a','b'),]

You can also use this:

DF[paste0("stu",c(2,3,5,9)), ]

Examples related to r

How to get AIC from Conway–Maxwell-Poisson regression via COM-poisson package in R? R : how to simply repeat a command? session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium How to show code but hide output in RMarkdown? remove kernel on jupyter notebook Function to calculate R2 (R-squared) in R Center Plot title in ggplot2 R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph R multiple conditions in if statement What does "The following object is masked from 'package:xxx'" mean?

Examples related to dataframe

Trying to merge 2 dataframes but get ValueError How to show all of columns name on pandas dataframe? Python Pandas - Find difference between two data frames Pandas get the most frequent values of a column Display all dataframe columns in a Jupyter Python Notebook How to convert column with string type to int form in pyspark data frame? Display/Print one column from a DataFrame of Series in Pandas Binning column with python pandas Selection with .loc in python Set value to an entire column of a pandas dataframe

Examples related to subset

how to remove multiple columns in r dataframe? Using grep to help subset a data frame in R Subset a dataframe by multiple factor levels creating a new list with subset of list using index in python subsetting a Python DataFrame Undefined columns selected when subsetting data frame How to select some rows with specific rownames from a dataframe? Subset data to contain only columns whose names match a condition find all subsets that sum to a particular value Subset and ggplot2

Examples related to rowname

Convert row names into first column Removing display of row names from data frame How to select some rows with specific rownames from a dataframe? Specifying row names when reading in a file Row names & column names in R