You are currently viewing How to Subset Vector in R?

Use R base bracket notation to subset the vector in R. By using this notation we can subset the vector by index, name, value, by checking the condition, by range etc. R also provides a subset() function to subsetting the vector by name and index. The subset() is a generic function that can be also used to subset dataframe and matrix.

Advertisements

This article explains different ways of subsetting vectors by starting with the simplest type of subsetting: subsetting an atomic vector with [] and extending by using other options, and learning how to subset a vector with positive integers, negative integers, a logical vector, or a character vector, and many more.

1. Quick Examples of Subset Vector

The following are quick examples of subsetting a vector and getting selected elements.


# Quick Examples

# Create a Vector
v <- c('A','B','C','D','E','F')

# Subset by Index
v[1]
v[-2]

# Subset by Range
v[2:4]

# Subset by list 
v[c(1,3)]
v[c(2.3,4.5)]

# Subset Vector by Negative Values
v[-c(5,2)]
v[c(-1,2)]

# Subset by Logical vector
v[c(TRUE,FALSE,TRUE,FALSE,TRUE,TRUE)]
v[c(TRUE,FALSE,TRUE,FALSE)]

# By Nothing
v[]

# By Zero
v[0]

# Named Vector
v <- c(c1='A',
      c2='B',
      c3='C',
      c4='D')

# Subset by Name
v[c('c2','c3')]

# subset by condition
v[v == 'B']
v[v %in% c('B','C')]

# Using subset
subset(v,subset=c(TRUE,FALSE))

2. Subset Vector By Index Position

To extract elements from a vector, use bracket notation ([]). This notation allows you to select a single element, multiple elements, or a range of elements from the vector. You can also use it to select elements from a list, among other operations. Let’s create a character vector with 5 elements and use [] notation to run these examples.


# Create a vector
v <- c('A','B','C','D','E','F')

# Subset by Index Position
v[1] 

# Output
#> v[1]
#[1] "A"

To subset multiple vector elements from the list in R, use the vector or list with the elements you want to select and use the object in [] notation. use c() to create a vector. If you use decimal numbers it just truncates to integer before using.


# Subset elements from list
v[c(1,3)]
v[c(2.3,4.5)]

# Output
#> v[c(1,3)]
#[1] "A" "C"
#> v[c(2.3,4.5)]
#[1] "B" "D"

3. Subset Vector by Negative Position

We can also negative integers to omit the elements from the specified options. The following examples ignore the values that are specified with the vectors and return the rest of the elements from the R vector.


# Subset Vector by Negative Values
v[-c(5,2)]
v[-c(2,3)]

# Output
#> v[-c(5,2)]
#[1] "A" "C" "D" "F"
#> v[c(-1,2)]
#Error in v[c(-1, 2)] : only 0's may be mixed with negative subscripts

4. By using logical vector

When we use a logical vector, it returns the values from a position where we have TRUE and ignores values from the FALSE position. If the logical vector is shorter than the vector being subsetted, it will return the vector with the same length as the input logical vector.


# Subset by Logical Vector
v[c(TRUE,FALSE,TRUE,FALSE,TRUE,TRUE)]
v[c(TRUE,FALSE,TRUE,FALSE)]

# Output
#> v[c(TRUE,FALSE,TRUE,FALSE,TRUE,TRUE)]
#[1] "A" "C" "E" "F"
#> v[c(TRUE,FALSE,TRUE,FALSE)]
#[1] "A" "C" "E"

5. By Nothing and Zero

By using [] notation with no value returns all elements from the vector and v[0] returns an empty vector.


# By Nothing
v[]

# By Zero
v[0]

# Output
#> v[]
#[1] "A" "B" "C" "D" "E" "F"
#> v[0]
#character(0)

6. Subset Vector by Name

If you have a named vector use a list with the names of the vector values to subset the vector by name in R. The following example returns named vectors c2 and c3.


# Named Vector
v <- c(c1='A',
      c2='B',
      c3='C',
      c4='D')

# Subset by Name
v[c('c2','c3')]

# Output
# c2  c3 
#"B" "C" 

7. Subset Vector by Condition

To subset vector by the logical condition in R using the condition in [] notation. To check vector values in a list of values use %in% the operator.


# Subset by condition
v[v == 'B']
v[v %in% c('B','C')]

# Output
#> v[v == 'B']
# c2 
#"B" 
#> v[v %in% c('B','C')]
# c2  c3 
#"B" "C" 

8. Using subset() Function

The subset() is a generic function from the R base that can be used to subset columns from vector in R. The following example uses the logical vector to select the vector elements.


# Using subset()
v <- c('A','B','C','D','E','F')
subset(v,subset=c(TRUE,FALSE))

# Output

9. Conclusion

In this article, you have learned how to subset the vector in R by using the R bracket [] notation and subset function. By using these I have explained subsetting vectors by name, index position, by condition, from the list, and many more examples. You can download this example from Github R Programming Examples Project.

References