Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R what does 2 commas mean?

Tags:

r

I'm looking at an example for the knnflex package and they setup a training and test set using the following:

train <- rbind(iris3[1:25,,1], iris3[1:25,,2], iris3[1:25,,3])
test <- rbind(iris3[26:50,,1], iris3[26:50,,2], iris3[26:50,,3])

My questions is how does this differ from :

train <- rbind(iris3[1:25,1], iris3[1:25,2], iris3[1:25,3])
test <- rbind(iris3[26:50,1], iris3[26:50,2], iris3[26:50,3])
like image 803
screechOwl Avatar asked Feb 23 '23 07:02

screechOwl


2 Answers

Two commas means there were more than two dimensions and you selected all of the items in the dimension that could have been specified between the two commas. For example, imagine a cube instead of a square, with all of the data in it. You can select row, height, and depth. If you select [row,,depth], then you will have selected an entire column in the cube at that row and depth. The principle is the same up to larger dimensions but harder to describe.

like image 184
John Avatar answered Mar 02 '23 15:03

John


Why don't you just try?

> train <- rbind(iris3[1:25,,1], iris3[1:25,,2], iris3[1:25,,3])
> test <- rbind(iris3[26:50,,1], iris3[26:50,,2], iris3[26:50,,3])
> train <- rbind(iris3[1:25,1], iris3[1:25,2], iris3[1:25,3])
Error in iris3[1:25, 1] : incorrect number of dimensions
> test <- rbind(iris3[26:50,1], iris3[26:50,2], iris3[26:50,3])
Error in iris3[26:50, 1] : incorrect number of dimensions

More generally, leaving an index unspecified selects all entries for that index:

> mtx<-matrix(c(1,2,3,4),nrow=2)
> mtx
     [,1] [,2]
[1,]    1    3
[2,]    2    4
> mtx[1,]
[1] 1 3
> mtx[,1]
[1] 1 2
like image 29
themel Avatar answered Mar 02 '23 16:03

themel