welcome: please sign in

Seiteninhalt hochladen

Sie können für die unten genannte Seite Inhalt hochladen. Wenn Sie den Seitennamen ändern, können Sie auch Inhalt für eine andere Seite hochladen. Wenn der Seitenname leer ist, leiten wir den Seitennamen vom Dateinamen ab.

Datei, aus der der Seiteninhalt geladen wird
Seitenname
Kommentar

Revision 2 vom 2015-03-15 09:49:00

location: RstatisTik / RstatisTikPortal / RcourSe / FinalFunction / indeXing

Indexing

Indexing with Positive Integers

   1 > letters[1:3]
   2 [1] "a" "b" "c"
   3 > letters[c(1:3,1:3)]
   4 [1] "a" "b" "c" "a" "b" "c"

   1 > x<-c(1,2,3,NA)
   2 > x[!is.na(x)]
   3 [1] 1 2 3

creates a vector without missing values. Also

   1 > x[is.na(x)] <- 0
   2 > x
   3 [1] 1 2 3 0

replaces the missing value by zeros. A common operation is to select rows or columns of data frame that meet some criteria. For example, to select those rows of painters data frame with Colour >= 17:

   1 > library(MASS)
   2 > painters[painters$Colour >= 17,]
   3 Composition Drawing Colour Expression School
   4 Bassano          6       8     17          0      D
   5 Giorgione        8       9     18          4      D
   6 Pordenone        8      14     17          5      D

We may want to select on more than one criterion. We can combine logical indices by the 'and', 'or' and 'not' operators. For example,

   1 > painters[painters$Colour >= 17 &  Composition Drawing Colour
   2 Titian             12      15     18
   3 Rembrandt          15       6     17
   4 Rubens             18      13     17
   5 Van Dyck           15      10     17

List of Logical Operations

Operation

Description

!

logical NOT

¦ | logical OR

<

less than

<=

less than or equal to

>

greater than

>=

greater than or equal to

==

logical equals (double =)

!=

not equal

¦¦

OR with IF

xor(x,y)

exclusive OR

isTRUE(x)

an abbreviation of identical(TRUE,x)

If we want to select a subgroup, for example those with schools A, B, and D. We can generate a logical vector using the <latex> \mathtt{\%in\%}</latex> operator as follows:

   1 > painters[painters$School %in% c("A","C","D"),]
   2 Da Udine           10       8     16        3      A
   3 Da Vinci           15      16      4       14      A
   4 Del Piombo          8      13     16        7      A

Sometimes we are interested in the indices of rows satisfying a certain condition. To extract these indices we use the which() command.

   1 > which(painters$School %in% c("A","C","D"))
   2 [1]  1  2  3  4  5  6  7  8  9 10 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
   3 [26] 32

Indexing with Character Vectors

A vector character strings with variable names can be used to extract those variables relevant for analysis. This is very useful when we have a large number of variables and we need to work with a few ones. For example,

   1 > names(painters)
   2 [1] "Composition" "Drawing" "Colour" "Expression" "School"
   3 > painters[1:3,c("Drawing","Expression")]
   4 Drawing Expression
   5 Da Udine         8          3
   6 Da Vinci        16         14
   7 Del Piombo      13          7

   1 > x <- c(1:3,NA)
   2 > names(x)<-letters[1:4]
   3 > x
   4 a  b  c  d
   5 1  2  3 NA
   6 > x[c("a","c")]
   7 a c
   8 1 3

Trimming Vectors Using Negative Indices

   1 > x<- c(5,8,6,7,1,5,3)
   2 > (z <- x[-1])
   3 [1] 8 6 7 1 5 3