In many functions I see things like data[, -i], if it is data[ , i] it mean refers to a particular column and all rows.Does that mean we are ommiting that column? data is a table that holds a csv file in my case.
Asked
Active
Viewed 1.1k times
-2
-
What makes you think it will ommit column `i`. Have you tried? Have you found some documentation? – flodel Oct 23 '13 at 16:24
-
im new to R. In one fuction it seemed to omit the particular column in the iteration.but in another it dint seem to. In most cases i felt it omited the column. – rama Oct 23 '13 at 16:25
-
1From the help of the `[` function (run ?'[' in your `R` session): "For ‘[’-indexing only: ‘i’, ‘j’, ‘...’ can be logical vectors, indicating elements/slices to select. Such vectors are recycled if necessary to match the corresponding extent. ‘i’, ‘j’, ‘...’ can also be negative integers, indicating elements/slices to leave out of the selection. " – Justin Oct 23 '13 at 16:26
-
http://stackoverflow.com/questions/7336679/in-r-what-does-cnt2-2-cnt2-1-do/7336746#7336746 – Ben Bolker Oct 23 '13 at 16:43
1 Answers
3
Using a - in [] does exactly that! Look at the example below and try other kinda examples yourself.
> a<-1:10
> a[-1]
[1] 2 3 4 5 6 7 8 9 10
> a[-(1:3)]
[1] 4 5 6 7 8 9 10
Works similarly with matrices:
> a<-matrix(1:4,2,2)
> a
[,1] [,2]
[1,] 1 3
[2,] 2 4
> a[,-1]
[1] 3 4
Have a great day!

user2217564
- 240
- 2
- 9