Table of Contents

R - Subset Operators (Extract or Replace Parts of an Object)

About

In the Data Manipulation category, you have the subset operators:

This operators acts on:

to extract or replace parts.

See also: the subset function

Usage

# Extraction
x[columnsSelector]
x[rowsSelector, columnsSelector, drop = ]
x[[rowsSelector, columnsSelector]] # shortcut can be used only to select one element (column, row) with drop = true 
x$name

# Replace
x[rowsSelector, columnsSelector] <- value

where:

2:3 # column 2 to 3
c("colA", "colC")

Demo Data

We will subset the following data frame

> df = data.frame(colA=1:5,colB=2:6,colC=3:7,row.names=letters[1:5])
> df
colA colB colC
a    1    2    3
b    2    3    4
c    3    4    5
d    4    5    6
e    5    6    7

Example for a list are available here

Subset Type

Column

One Column

df$colA
[1] 1 2 3 4 5

df[,2]  # return an vector integer because the default is to have drop = true for a column (not for a row)
df[,2, drop = TRUE] # same 
[1] 2 3 4 5 6

df[2]
df[,2,drop = FALSE] # same
colB
a    2
b    3
c    4
d    5
e    6

df[,"colB"]
[1] 2 3 4 5 6

df[-2]
colA colC
a    1    3
b    2    4
c    3    5
d    4    6
e    5    7

Multiple Columns

# By naming
df[,c("colA","colB")]
# or
df[c("colA","colB")]
# By Indexing
df[,c(1,2)]
# or
df[c(1,2)]
colA colB
a    1    2
b    2    3
c    3    4
d    4    5
e    5    6

df[2:3]
# or
df[,2:3]
colB colC
a    2    3
b    3    4
c    4    5
d    5    6
e    6    7

df[-c(1,2)]
colC
a    3
b    4
c    5
d    6
e    7

Row

One Row

df[4,]
colA colB colC
d    4    5    6

df[4,,drop=FALSE]
colA colB colC
d    4    5    6

df["d",]
colA colB colC
d    4    5    6

Multiple Rows Indexing

df[c("b","e"),]
colA colB colC
b    2    3    4
e    5    6    7

df[c(TRUE,FALSE,TRUE,FALSE,FALSE),]
colA colB colC
a    1    2    3
c    3    4    5

Multiple Rows Filtering

df[df$colB>3,]
# of
df[df[,"colB"]>3,]
# of
df[df[,2]>3,]
colA colB colC
c    3    4    5
d    4    5    6
e    5    6    7

df[df$colB>3&df$colC<=6,]
colA colB colC
c    3    4    5
d    4    5    6

Vertical and Horizontal

df[2:4,2:3]
colB colC
b    3    4
c    4    5
d    5    6

res[res$SUCCESS_FLG==3 & grepl("46066",res$ERROR_TEXT),c("TOTAL_TIME_SEC")] <-200

Documentation / Reference

?":"
?"["
?"$"
?"[["
?"[.data.frame"