The Art of R Programming
Here, the 1 value was recycled into a vector of four 1 values. You can also use therbind()andcbind()functions as a quick way to ...
Here, we’ll look at the common example of finding distances between cities, as it is easier to describe than, say, finding dista ...
among those minima. But as you’ll see, the code logic becomes rather intricate. One key point is that the matrix issymmetric, be ...
to entry 3 in row 1 of the output, finding 4. So, the pair of cities closest to each other is city 3 and city 4. Lines 9 and 10 ...
Aszis still a vector, we can query its length: length(z) [1] 8 But as a matrix,zis a bit more than a vector: class(z) [1] "ma ...
3.6 Avoiding Unintended Dimension Reduction.................................... In the world of statistics, dimension reduction ...
Fortunately, R has a way to suppress this dimension reduction: thedrop argument. Here’s an example, using the matrixzfrom above: ...
>z ab [1,] 1 3 [2,] 2 4 > colnames(z) [1] "a" "b" > z[,"a"] [1]12 As you see here, these names can then be used to refe ...
and two columns within each layer. We’ll storefirsttestin the first layer and secondtestin the second. In layer 1, there will be ...
...
4 LISTS In contrast to a vector, in which all ele- ments must be of the same mode, R’slist structure can combine objects of diff ...
components cannot be broken down into smaller components. In contrast, lists are referred to asrecursivevectors. For our first l ...
Since lists are vectors, they can be created viavector(): > z <- vector(mode="list") > z[["abc"]] <- 3 >z $abc [1 ...
another list—a sublist of the original. For instance, continuing the preced- ing example, we have this: > j[1:2] $name [1] "J ...
$b [1] 12 z$c <- "sailing" # add a c component did c really get added? z $a [1] "abc" $b [1] 12 $c [1] "sailing" Adding com ...
$c [1] "sailing" [[3]] [1] 28 [[4]] [1] FALSE [[5]] [1] TRUE [[6]] [1] TRUE Note that upon deletingz$b, the indices of the eleme ...
Suppose our input file,testconcord.txt, has the following contents (taken from this book!): The [1] here means that the first it ...
The list consists of one component per word in the file, with a word’s component showing the positions within the file where tha ...
[21] "of" "only" "one" "line" "and" [26] "one" "item" "so" "this" "is" [31] "redundant" "but" "this" "notation" "helps" [36] "to ...
On the other hand, if we were to start with numbers, we would get numbers. > z <- list(a=5,b=12,c=13) > y <- unlist( ...
«
1
2
3
4
5
6
7
8
9
10
»
Free download pdf