RSS

Category Archives: Basics

R Basics


R console is very powerful. It executes every valid command of R.Type the below command on console, you will get the output.

> getwd() – Get the present working directory.

> setwd(“C://Ajay”) – Change the working directory to defined one.

> 2*3
[1] 6

> 7*4
[1] 28

> 7/4
[1] 1.75

> as.integer(7/4) – Data type conversion. We can convert the data types by using as keyword.
[1] 1

> 5 + 3 * 7 / 2 ^ 3 – 7 – Automatically follows the BODMAS rule.
[1] 0.625

> log(2)
[1] 0.6931472

> log(7)
[1] 1.94591

> exp(1)
[1] 2.718282

> exp(7)
[1] 1096.633

> factorial(5)
[1] 120

> sqrt(2)
[1] 1.414214

> pi
[1] 3.141593

> seq(1,10, by=2) – It generates the sequence of numbers.
[1] 1 3 5 7 9

Generate random numbers between specific set of numbers, we have runif. Every time we run the above statement, it produces the different result set between min and max numbers.

> runif(10,1,5)
[1] 2.328478 1.483477 2.891761 1.521750 3.812675 3.244088 3.219541 2.077398 2.410314
[10] 3.368466

Sometimes, we have to select some random sample from our data set, To select the subset from the set, we can use sample.

> x=seq(1,100)
> sample(x, 10)
[1] 92 53 34 20 85 40 39 21 38 73
> sample(x, 10)
[1] 16 18 80 3 75 7 90 36 42 41
> sample(x, 10)
[1] 30 75 87 2 70 64 69 89 40 35

To concatenation the two strings, we have paste command

paste(“A”, 1:6, sep = “”)
[1] “A1” “A2” “A3” “A4” “A5” “A6”

> paste(“A”, 1:6, sep = “,”)
[1] “A,1” “A,2” “A,3” “A,4” “A,5” “A,6”

To replace the string with other string, we have gsub command, we can also use gsub to replace data within data frame.

> a=”I love R”
> gsub(“I”, “We”, a)
[1] “We love R”

a=c(1,2,3,4,5,6,1,2,43,4,5,1,2,34,5)
> gsub(“1”, “11”, a)
[1] “11” “2” “3” “4” “5” “6” “11” “2” “43” “4” “5” “11” “2” “34” “5”

> age=12:20 – Create a sequence of number starting from 12
> height=c(48, 47, 70, 50, 62, 60, 62, 72, 55) – Creates an height vector
> height[3]=42 – replace third element with 42
> students = data.frame(age=age, height=height) – Create a data frame and bind both age and height. Age and Height dimension should be same.
> View(students) – To view the newly created data frame.

To know the data type of objects, you created, we can use class

> class(students$age)
[1] “integer”
> class(students)
[1] “data.frame”

> head(students) – It displays the first six rows of data frame.

> head(students, n) – It displays the first n rows of data frame.

> tail(students) – It displays the last six rows of data frame.

> tail(students, n) – It displays the last n rows of data frame.

> summary(students) – It generates the summary i.e. min, max, mean, median quantiles of each column.
age                  height
Min. :12           Min. :42.00
1st Qu.:14       1st Qu.:48.00
Median :16       Median :55.00
Mean :16         Mean :55.33
3rd Qu.:18       3rd Qu.:62.00
Max. :20          Max. :72.00

>ls() – It displays the list of live objects.

 
Leave a comment

Posted by on August 19, 2013 in Basics

 

Tags: ,