Wednesday, October 2, 2013

R programming: Vector management

Create vector with values 1 to 10
Use: v <- seq(1, 10)
Use: v <- 1:10

Create vector of numbers from 10 to 20 and 30 to 40
Use: v <- c(10:20, 30:40)

Create vector of size 10 initialized to zero.
Use: v <- rep(0,10)

Create vector of 5, 10, 15 ... 100
Use:  v <- seq(5, 50, 5)













Append value 1 at the end of vector
Use: v <- append(v, 1)

Append another vector at the end of vector
Use: v <- append(v, v)









Count zero values from vector
Use: sum(v == 0) 

Subset with non-zero values
Use: v <- subset(v, v != 0)
Use: v <- v[which(v != 0)]














Find common elements from two vectors
Use: v <- intersect(a, b)

Find index of common elements
Use: which(a%in%b)

Count number of characters at each position
Use: nchar(v)

Create subset based on length of value at each element
Use: v <- v[nchar(v) < 5]


No comments:

Post a Comment