R Programming and Lab - Unit III pptxs

saravanakumarss3 10 views 14 slides Mar 06, 2025
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

R Programming and Lab - Unit III pptxs


Slide Content

R Programming – UNIT III S. S. SARAVANA KUMAR ASSISTANT PROFESSOR Department of Computer Applications Sri Ramakrishna College of Arts and Science Coimbatore - 641 006 Tamil Nadu, India

Lists and Frames A list is , a vector where each element can be of a different type. Data Frames Data frames are used to store spreadsheet-like data. They can either be thought of as matrices where each column can store a different type of data, or non-nested lists where each element is of the same length.

Creating Lists Lists are created with the list function, and specifying the contents works much like the c function that we’ve seen already . Lists are the R objects which contain elements of different types like − numbers, strings, vectors and another list inside it. A list can also contain a matrix or a function as its elements. List is created using list() function. ( a_list <- list( c(1, 1, 2, 5, 14, 42), month.abb , matrix(c(3, -8, 1, -3), nrow = 2), asin ))

Creating Lists # Create a list containing strings, numbers, vectors and a logical # values. list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1) print( list_data ) [[1]] [1] "Red " [[2]] [1] "Green " [[3]] [1] 21 32 11 [[4]] [1] TRUE [[5]] [1] 51.23 [[6]] [1] 119.1

Atomic and Recursive Variables Ability to contain other lists within themselves, lists are considered to be recursive variables . Vectors, matrices, and arrays, by contrast, are atomic. (Variables can either be recursive or atomic, never both) The functions is.recursive and is.atomic let us test variables to see what type they are: is.atomic (list()) ## [1] FALSE is.recursive (list()) ## [1] TRUE is.atomic (numeric()) ## [1] TRUE is.recursive (numeric()) ## [1] FALSE

List Dimensions and Arithmetic Like vectors, lists have a length. A list’s length is the number of top-level elements that it contains: length( a_list ) ## [1] 4 length( main_list ) #doesn't include the lengths of nested lists ## [1] 2 The dim function correspondingly returns NULL: dim( a_list ) ## NULL

List Dimensions and Arithmetic nrow , NROW, and the corresponding column functions work on lists in the same way as on vectors: nrow ( a_list ) ## NULL ncol ( a_list ) ## NULL NROW( a_list ) ## [1] 4 NCOL( a_list ) ## [1] 1 l1 <- list(1:5) l2 <- list(6:10) l1[[1]] + l2[[1]] ## [1] 7 9 11 13 15

Indexing Lists Consider this test list: l <- list( first = 1, second = 2, third = list( alpha = 3.1, beta = 3.2 ) ) l[1:2] ## $first ## [1] 1 ## ## $second ## [1] 2 l[-3] ## $first ## [1] 1 ## ## $second ## [1] 2 l[c("first", "second")] ## $first ## [1] 1 ## ## $second ## [1] 2 l[c(TRUE, TRUE, FALSE)] ## $first ## [1] 1 ## ## $second ## [1] 2

Combining Lists The c function that we have used for concatenating vectors also works for concatenating lists: c(list(a = 1, b = 2), list(3)) ## $a ## [1] 1 ## ## $b ## [1] 2 ## ## [[3]] ## [1] 3 c(list(a = 1, b = 2), 3) ## $a ## [1] 1 ## ## $b ## [1] 2 ## ## [[3]] ## [1] 3

NULL (bank_holidays_2013 <- list( Jan = "New Year's Day", Feb = NULL, Mar = "Good Friday", Apr = "Easter Monday", May = c("Early May Bank Holiday", "Spring Bank Holiday"), Jun = NULL, Jul = NULL, Aug = "Summer Bank Holiday", Sep = NULL, Oct = NULL, Nov = NULL, Dec = c("Christmas Day", "Boxing Day") ))

NULL ## $Jan ## [1] "New Year's Day" ## ## $Feb ## NULL ## ## $Mar ## [1] "Good Friday" ## ## $Apr ## [1] "Easter Monday" ## ## $May ## [1] "Early May Bank Holiday" "Spring Bank Holiday " ## ## $Jun ## NULL ## ## $Jul ## NULL ## ## $Aug ## [1] "Summer Bank Holiday" ## ## $Sep ## NULL ## ## $Oct ## NULL ##

Frames Creating Data Frames We create data frames with the data.frame function: ( a_data_frame <- data.frame ( x = letters[1:5], y = rnorm (5), z = runif (5) > 0.5 )) ## x y z ## 1 a 0.17581 TRUE ## 2 b 0.06894 TRUE ## 3 c 0.74217 TRUE ## 4 d 0.72816 TRUE ## 5 e -0.28940 TRUE class( a_data_frame ) ## [1] " data.frame "

Frames Creating Data Frames y <- rnorm (5) names(y) <- month.name[1:5] data.frame ( x = letters[1:5], y = y, z = runif (5) > 0.5 ) ## x y z ## January a -0.9373 FALSE ## February b 0.7314 TRUE ## March c -0.3030 TRUE ## April d -1.3307 FALSE ## May e -0.6857 FALSE

Indexing Data Frames There are lots of different ways of indexing a data frame. To start with, pairs of the four different vector indices (positive integers, negative integers, logical values, and characters ) can be used in exactly the same way as with matrices. These commands both select the second and third elements of the first two columns: a_data_frame [2:3, -3] ## x y ## 2 b 0.06894 ## 3 c 0.74217 a_data_frame [c(FALSE, TRUE, TRUE, FALSE, FALSE), c("x", "y")] ## x y ## 2 b 0.06894 ## 3 c 0.74217
Tags