Reputation:
I was debugging a code I found online, in order to debug it. One of the line is:
z = train[train[,11]==i, 1:10]
where train
is defined as:
train = matrix(0,nrow(y)/2,ncol(y))
and y
is the input data from a .csv file (in the matrix form). The code is for training a classifier from the input data given in the .csv form. The file contains only 0
s and 1
s.
Wouldn't train[,11]==i
return a Boolean value? How is the train
matrix being accessed by a Boolean parameter?
Upvotes: 0
Views: 60
Reputation: 2812
Lets break it down:
# Create some data and a value to look for
> train <- matrix(1:121,11)
> i = 112
# This is a vector of column 11 of the matrix
> train[,11]
[1] 111 112 113 114 115 116 117 118 119 120 121
# This is a vector of the same length as column 11 of the
# matrix with TRUE/FALSE depending on if the value equals i
> train[,11]==i
[1] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# Finally, this is then selecting the rows that had a TRUE
# value. So if the 2nd item in the column above is 112 (it is)
# then only the second value in the vector will be TRUE, so it
# will select the second row (could be more rows with different
# data in the matrix of course). It also selects only the first
# ten columns of that row, i.e. 1:10.
> train[train[,11]==i, 1:10]
[1] 2 13 24 35 46 57 68 79 90 101
Basically, you can use a boolean vector to select. It will also wrap as you would expect, so this next example will use TRUE then FALSE, then TRUE then FALSE, until the length 10 is reached.
x <- 1:10
x[c(T,F)]
Upvotes: 2