0% found this document useful (0 votes)
17 views13 pages

Cost Lab 1

Uploaded by

swapnikte
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views13 pages

Cost Lab 1

Uploaded by

swapnikte
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

AIM: Using R execute .

SOURCE CODE & OUTPUT:

1. Hello world program:

2. R-Datatypes:
i. Logical
ii. Numeric
iii. Integer
iv. Complex
v. Character
vi. Raw
3. R — Vectors:

 Vector Creation

I. Using Colon Operator:

iii. Using c() Function:


4. R Lists:
• Creating R-List:
Accessing Vector Elements:
i. Using Position:

Using Logical Indexing:

Using Negative Indexing:

 Naming Rows and Columns of Matrix:


 Accessing Elements of Matrix:

6. R-Arrays:
• Creating R - Array:
 Naming Dimensions of Array

• Accessing Elements of Array:


7. R-Factors:
• Creating Factors and Finding Number of Distinct Values:

8. R Data Frames:
Creating R-Data Frames:

# Creating Data Frame

Getting Structure of Data Frame


> # Creating Data Frame

> employee<- data.frame (emp_id=c (1001, 1002, 1003, 1004, 1005),


emp_name=c('Sadik', 'Pinky', 'Manoj', 'Krishna', 'Sonam'), salary=c (56000, 49000,
45000, 35000,28000), DOJ=as.Date (c('2012-8-07','2013-01-15','2013-06-08','2014-
11-10','2015-06-05')), stringsAsFactors=FALSE)

> print (employee)

emp_id emp_name salary DOJ

1 1001 Sadik 56000 2012-08-07

2 1002 Pinky 49000 2013-01-15

3 1003 Manoj 45000 2013-06-08

4 1004 Krishna 35000 2014-11-10

5 1005 Sonam 28000 2015-06-05


Getting Statistical Summary
> # Creating Data Frame

> employee<- data.frame (emp_id=c(1001, 1002, 1003, 1004, 1005),


emp_name=c('Sadik', 'Pinky', 'Manoj', 'Krishna', 'Sonam'), salary=c (56000, 49000,
45000, 35000,28000),DOJ=as.Date(c('2012-08-07', '2013-01-15', '2013-06-08',
2014-11-10', '2015-06-05')),stringsAsFactors=FALSE)

Error: unexpected string constant in "employee<- data.frame (emp_id=c(1001,


1002, 1003, 1004, 1005), emp_name=c('Sadik', 'Pinky', 'Manoj', 'Krishna', 'Sonam'),
salary=c (56000, 49000, 45000, 35000,28000),DOJ=as.Date(c('2012-08-07'"

> print (employee)

emp_id emp_name salary DOJ

1 1001 Sadik 56000 2012-08-07

2 1002 Pinky 49000 2013-01-15

3 1003 Manoj 45000 2013-06-08

4 1004 Krishna 35000 2014-11-10

5 1005 Sonam 28000 2015-06-05


Extracting Data from Data Frame:
# Creating Data Frame

> employee<- data.frame (emp_id=c (1001, 1002, 1003, 1004, 1005),


emp_name=c('Sadik', 'Pinky', 'Manoj', 'Krishna', 'Sonam'), salary=c (56000, 49000,
45000, 35000, 28000),DOJ=as.Date (c ('2012-08-07', '2013-01-15', '2013-06-08',
'2014-11-10', '2015-06-05')),stringsAsFactors=FALSE)

>

> # Extracting emp_name and DOJ from employee

> print (data.frame (employee$emp_name, employee$DOJ))

employee.emp_name employee.DOJ

1 Sadik 2012-08-07

2 Pinky 2013-01-15

3 Manoj 2013-06-08

4 Krishna 2014-11-10

5 Sonam 2015-06-05

>

> # Extracting emp_id and salary from employee

> print (employee [,c(1,3)])

emp_id salary

1 1001 56000

2 1002 49000

3 1003 45000

4 1004 35000

5 1005 28000

>

> # Extracting first three rows from employee

> print (employee [1:3,1])


[1] 1001 1002 1003

>

> # Extracting 2nd and 5th row with 2nd and 4th column

> print (employee [c (2,5), c(2, 4)])

emp_name DOJ

2 Pinky 2013-01-15

5 Sonam 2015-06-05

>

> # Expanding Data Frames

>

> # Creating Data Frame

> employee<- data.frame (emp_id=c(1001, 1002, 1003, 1004, 1005),


emp_name=c('Sadik', 'Pinky', 'Manoj', 'Krishna', 'Sonam'), salary=c (56000, 49000,
45000, 35000,28000),DOJ=as.Date(c('2012-08-07', '2013-01-15', '2013-06-08',
'2014-11-10', '2015-06-05')),stringsAsFactors=FALSE)

>

> # Adding Depatment Column to employee

> employee$Department<-c('Finance', 'HR', 'Operations', 'IT', 'IT')

> print (employee)

emp_id emp_name salary DOJ Department

1 1001 Sadik 56000 2012-08-07 Finance

2 1002 Pinky 49000 2013-01-15 HR

3 1003 Manoj 45000 2013-06-08 Operations

4 1004 Krishna 35000 2014-11-10 IT

5 1005 Sonam 28000 2015-06-05 IT

>
Adding Rows:
# Expanding Data Frames

>

> # Creating Data Frame

> employee<- data.frame (emp_id=c (1001, 1002, 1003,1004, 1005),


emp_name=c('Sadik', 'Pinky', 'Manoj', 'Krishna', 'Sonam'), salary=c (56000, 49000,
45000, 35000, 28000),

+ DOJ=as.Date(c('2012-08-07', '2013-01-15', '2013-06-08','2014-11-10', '2015-06-


05')),Department=c('Finance', 'HR', 'Operations', 'IT', 'IT'), stringsAsFactors=FALSE)

>

> # Adding Rows to employee using rbind()

>

> # Creating Second Data Frame

> employeeNew<- data.frame(emp_id=c (1006, 1007, 1008), emp_name=c('Shruti',


'Pawan', 'Raj'),salary=c (46000, 34000, 32000),DOJ=as.Date(c('2015-10-07', '2015-
10-15', '2014-01-08')),

+ Department=c ('Finance', 'Operations', 'IT'),stringsAsFactors=FALSE)

>

> #Binding Data Frames

> employee2<-rbind (employee, employeeNew)

> print (employee2)

emp_id emp_name salary DOJ Department

1 1001 Sadik 56000 2012-08-07 Finance

2 1002 Pinky 49000 2013-01-15 HR

3 1003 Manoj 45000 2013-06-08 Operations

4 1004 Krishna 35000 2014-11-10 IT


5 1005 Sonam 28000 2015-06-05 IT

6 1006 Shruti 46000 2015-10-07 Finance

7 1007 Pawan 34000 2015-10-15 Operations

8 1008 Raj 32000 2014-01-08 IT

>
Lab 2
AIM: Create a Matrix using R and Perform the operations addition, inverse, transpose and multiplication operations.
SOURCE CODE & OUTPUT:

You might also like