0

I see a lot of these [,something]

  1. filename.train <- train[indexes,]
  2. x <- dataset[,1:4]
FIC
  • 41
  • 1
  • 4
  • 2
    Please consider [taking the tour](https://stackoverflow.com/tour). Users are more likely to help if you (1) do some [research yourself](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), and (2) learn [how to ask](https://stackoverflow.com/help/how-to-ask) questions. This is a very basic R question. I recommend any introductory R tutorial/text. SO is not a good tutorial service, and questions such as yours usually attract down votes very quickly. – Maurits Evers Oct 30 '18 at 05:59
  • You should look up indexing for r – Zito Relova Oct 30 '18 at 06:02
  • https://stat.ethz.ch/R-manual/R-patched/library/base/html/Extract.html – r2evans Oct 30 '18 at 06:08
  • This book chapter on subsetting will explain it all to you https://adv-r.hadley.nz/subsetting.html very well – prosoitos Oct 30 '18 at 06:15
  • Duplicate of [How to index an element of a list object in R](https://stackoverflow.com/questions/21091202/how-to-index-an-element-of-a-list-object-in-r) – Maurits Evers Oct 30 '18 at 06:29

1 Answers1

2

The [,] syntax is used for indexing. Your dataset is a so-called data.frame which implies a rectangular shape and consists of rows and columns. You can index every value of your dataframe by indicating which row(s)/column(s) you want to be returned. This is done by using the [,] syntax: [rows you want, columns you want]. If you want all rows to be returned, you simply do not define any rows - you leave it blank. For example

    dataset[,4]

returns the fourth column and all rows from your dataframe. You can also get multiple rows/columns by defining multiple indices in [,]. You can for example use 1:4 to get all the first 4 rows (1:4 is the syntax for a sequence from 1 to 4):

    train[1:4,]

Note that this returns all columns since you did not specify any column indices.
You could also combine the indexing for rows and columns:

    train[2:5, 7:9]

will return the rows 2-5 and columns 7-9. In general what the [,] does is called subsetting, because it generates a subset of columns and rows from your dataframe. Internally R calls a function called subset which does the actually subsetting.

FloSchmo
  • 723
  • 5
  • 9