Beginning R with some really simple vector manipulations

Cookbook-r is a really cool place to start R. Still I have some simple scripts that you may want to start with.

Repeat a number:

> rep(3,5)
[1] 3,3,3,3,3

Get a slice of a vector:

> v <- 1:10
> s <- c[2,3]
> x <- v[s]
> x
[1] 1,2

Replacing every element in a vector logically

> v <- 1:10
> v[v<3] <- 5
> v
[1] 5 5 3 4 5 6 7 8 9 10

Evaluate a whole vector for a logical operation

> v <- 1:5
> v < 10
[1] True True True True True

Creating a matrix

> u <- matrix(1:10,nrow=5)
> u
 [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10

Working with simple data-frame

> df1 <- data.frame(1:4,rep(3,4),c(2,4,1,5))
> df1
  X1.4 rep.3..4. c.2..4..1..5.
1    1         3             2  
2    2         3             4  
3    3         3             1  
4    4         3             5  

Manipulating the column names of the data-frame:

> colnames(df1)
[1] "X1.4"          "rep.3..4."     "c.2..4..1..5."
> colnames(df1) <- c("c1","c2","c3")
> df1
  c1 c2 c3
1  1  3  2  
2  2  3  4  
3  3  3  1  
4  4  3  5

You can retrieve the 2nd column in two different ways, first by the index and second based on the name it is called:

> df1[,2]
[1] 3 3 3 3

> df1$c2
[1] 3 3 3 3

Adding some more columns in the date frame:

> df1 <- cbind(df1,c("truck","car","lettuce","chop"))
> df1
  c1 c2 c3 c("truck", "car", "lettuce", "chop")
1  1  3  2                                truck  
2  2  3  4                                  car  
3  3  3  1                              lettuce  
4  4  3  5                                 chop  

Giving a nice name to the last added column:

colnames(df1)[4] <- "things"  
> colnames(df1)[4] <- "things"
> df1
  c1 c2 c3  things
1  1  3  2   truck  
2  2  3  4     car  
3  3  3  1 lettuce  
4  4  3  5    chop  

Subsetting the data-frame, e.g. taking first 3 rows of last two variables in the data-frame

> newdf[1:3,3:4]
  c3  things
1  2   truck  
2  4     car  
3  1 lettuce  

Saving the data-frame in the current working directory:

write.table(df1,file="testdata.csv",row.names=FALSE,sep=",")  

Importing the same data-frame you just saved:

> newdf <- read.table("testdata.csv",header = TRUE,sep=",",stringsAsFactor=FALSE)
> newdf
  c1 c2 c3  things
1  1  3  2   truck  
2  2  3  4     car  
3  3  3  1 lettuce  
4  4  3  5    chop  

Saving all the variables and stuffs in the current workspace in the current working directory:

> save.image()

Loading the saved workspace in the current working directory:

> load(".RData")

Clearing the workspace:

rm(list=ls())  

Creating a sequence of Dates

> dates <- seq.Date(as.Date("2016/01/11"),as.Date("2016/01/15"),length.out=5)
> dates
[1] "2016-01-11" "2016-01-12" "2016-01-13" "2016-01-14" "2016-01-15"
Show Comments