R | Slicing and Extracting Data

Indexing Vectors

  • x[n] nth element
  • x[-n] all but the nth element
  • x[1:n] first n elements
  • x[-(1:n)] elements from n+1 to the end
  • x[c(1,4,2)] specific elements
  • x[“name”] element named “name”
  • x[x > 3] all elements greater than 3
  • x[x > 3 & x < 5] all elements between 3 and 5
  • x[x %in% c(“a”,”and”,”the”)] elements in the given set

Indexing Lists

  • x[n] list with elements n
  • x[[n]] nth element of the list
  • x[[“name”]] element of the list named “name”
  • xname</strong> id.</li> </ul> <h3>Indexing Matrices</h3> <ul> 	<li><strong>x[i,j]</strong> element at row i, column j</li> 	<li><strong>x[i,]</strong> row i</li> 	<li><strong>x[,j]</strong> column j</li> 	<li><strong>x[,c(1,3)]</strong> columns 1 and 3</li> 	<li><strong>x["name",]</strong> row named "name"</li> </ul> <h3>Indexing Data Frames</h3> <ul> 	<li><strong>x[["name"]]</strong> column named "name"</li> 	<li><strong>xname id