Skip to content

Instantly share code, notes, and snippets.

@prasoonsharma
Created November 26, 2010 04:07
Show Gist options
  • Save prasoonsharma/716272 to your computer and use it in GitHub Desktop.
Save prasoonsharma/716272 to your computer and use it in GitHub Desktop.
Data structures in R
# DATA STRUCTURES IN R
# Vector: a sequence of elements of same atomicity
# Vectors can be formed by using the concatenate function c()
alphabets <- c("a", "b", "c")
alphabets
numbers <- c(1, 2, 3)
numbers
# Matrix: 2 dimensional vector
m <- matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3, ncol = 3)
m
# data can be recyled i.e. if fewer elements are specified than the container matrix, then data is recycled
m <- matrix(c(1, 2, 3), nrow = 3, ncol = 3)
m
?matrix
# Array: multi-dimensional matrix
?array
# Data frames: ~ a RDBMS table (~ matrix but only columns must be of same data type)
id <- c(1,2,3)
name <- c("John", "Kirk", "AJ")
age <- c(21,30,18)
employees <- data.frame(id,name,age)
names(employees) <- c("ID","Name","Age") # column names
# List: an ordered collection of objects
temp <- list(name="Fred", phone.numbers=c("201.121.1212","908.111.1121"), state="New Jersey")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment