Skip to content

Instantly share code, notes, and snippets.

@sankars
Created April 11, 2014 16:23
Show Gist options
  • Select an option

  • Save sankars/10481803 to your computer and use it in GitHub Desktop.

Select an option

Save sankars/10481803 to your computer and use it in GitHub Desktop.
Vector operations
# http://stackoverflow.com/questions/7706876/r-script-removing-na-values-from-a-vector
# Sort vectors that have 'NA'
d <- c(1, 100, NA, 10)
max(d, na.rm=TRUE)
# Remove 'NA' in a vector
d <- d[!is.na(d)]
na.omit(d)
# http://stackoverflow.com/questions/3413879/how-to-create-an-empty-r-vector-to-add-new-items
# Create empty vector
d <- vector()
d <- rep(NA, 10) # preallocate a vector with
d <- {}
d <- c()
d <- NULL
# http://stackoverflow.com/questions/4570537/vectorizing-a-matrix
# create a vector from matrix
m <- matrix(1:9,3)
c(m)
as.vector(m)
# http://stackoverflow.com/questions/2545228/converting-a-dataframe-to-a-vector-by-rows
# convert a data frame to vector
d <- data.frame(x=c(26,21,20),y=c(34,29,28))
v <- c(t(d)) # t implicitly converts data frame to matrix
v <- as.vector(t(d))
v <- as.vector(as.matrix(d)) # convert by column
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment