1 / 10

S-PLUS Lecture 4

S-PLUS Lecture 4. Jaeyong Lee Pennsylvania State University. Lists 1. A list is an object consisting of objects called components. The components of a list don’t need to be of the same mode or type and they can be a numeric vector, a logical value and a function and so on.

london
Download Presentation

S-PLUS Lecture 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. S-PLUS Lecture 4 Jaeyong Lee Pennsylvania State University

  2. Lists 1 • A list is an object consisting of objects called components. • The components of a list don’t need to be of the same mode or type and they can be a numeric vector, a logical value and a function and so on. • A component of a list can be referred as aa[[I]] or aa$times, where aa is the name of the list and times is a name of a component of aa.

  3. Lists 2 • The names of components may be abbreviated down to the minimum number of letters needed to identify them uniquely. • aa[[1]] is the first component of aa, while aa[1] is the sublist consisting of the first component of aa only. • There are functions whose return value is a List. We have seen some of them, eignen, svd, …

  4. Lists: Session Empl <- list(employee=“Anna”, spouse=“Fred”, children=3, child.ages=c(4,7,9)) Empl[[4]] Empl$child.a Empl[4] # a sublist of Empl consisting of the 4th component of Empl names(Empl) <- letters[1:4] Empl <- c(Empl, service=8) unlist(Empl) # converts it to a vector. Mixed types will be converted # to character, giving a character vector.

  5. Data Frames 1 • A data frame is a list with class “data.frame”. There are restrictions on lists that may be made into data frames. a. The components must be vectors (numeric, character, or logical), factors, numeric matrices, lists, or other data frames. b. Matrices, lists, and data frames provide as many variables to the new data frame as they have columns, elements, or variables, respectively. c. Numeric vectors and factors are included as is, and non-numeric vectors are coerced to be factors, whose levels are the unique values appearing in the vector. d. Vector structures appearing as variables of the data frame must all have the same length, and matrix structures must all have the same row size.

  6. Data Frames 2 • Data frames may be treated in many ways as a matrix possibly of differing modes and attributes. It may be displayed in matrix form, and its columns and rows extracted using matrix indexing conventions.

  7. Data Frames: Session citizen <-data.frame(income=c(10,12,22), nationality=c("us","uk","ch")) Attributes(citizen$nationality) # character vector is # coerced to be a factor. citizen <- data.frame(citizen, mat=matrix(c(1,2,3,4,5,6),nr=3)) row.names(citizen) <- c(“alan”, “smith”, “zhang”) names(citizen)[3:4] <- c("a","b") citizen[1:2,1:2]

  8. Reading Data from Files: Session bb <- scan(file="b.data") bb <- matrix(scan(file=“b.data”), byrow=T, nc=3) aa <- scan(file=“a.data”, list(0,””)) aa[[1]] aa[[2]] morley <- read.table(file=“morley.data”)

  9. Control Structures: Session 1 x <- 1:9 if (length(x) <= 10) { x <- c(x,10:20);print(x) } if (length(x) < 5) print(x) else print(x[5:20]) for(i in 1:10) {x[i] <- rnorm(1)} j <- 1 While( j < 10) { print(j) j <- j + 2 }

  10. Control Structure: Session 2 j < -1 repeat { print(j) j <- j + j/3 if (j > 30) break }

More Related