New to R programming, request assistance with a rbind command

I have a class assignment and need help on the rbind command using a negative 1 in the reference.
I was given an input file which I read in and called it digits.
file is 42000 rows and 700 columns.
I created and populated a vector called digits.0 which has the row number from the input file that matches the selection criteria (in this example ==0).
digits.0 is one column and over 4000 rows.
Once digits.0 is created via the for loop, the next command creates a new vector using rbind with a reference to my input file where the reference uses -1 for a column. What does the -1 do?

digits.0<-c(rep(0,4132))

k<-1
for(i in 1:42000){
  if(digits[i,1]==0){
    digits.0[k]<-i
    k<-k+1
  }
}
digits.0data<- rbind(digits[digits.0,-1])

I would appreciate a quick reply or an online chat if possible.

John2

The -1 mean "keep all of the columns except column 1".

DF <- data.frame(A = c(1,0,0,1,0), B = c(0,0,1,1,0), C = c(1,1,0,1,0))
DF
#>   A B C
#> 1 1 0 1
#> 2 0 0 1
#> 3 0 1 0
#> 4 1 1 1
#> 5 0 0 0
DF[ , -1]
#>   B C
#> 1 0 1
#> 2 0 1
#> 3 1 0
#> 4 1 1
#> 5 0 0

Created on 2020-03-25 by the reprex package (v0.3.0)

Wow, that makes perfect sense.
Now that you provided the answer, it seems obvious.
I have also confirmed that is what is happening with my vectors.

Thank you for the quick reply.

follow up question: the vector created = digits.0 has length of 4132 with each entry indicating the row location in column 1 of the input file where the digit 0 was found.
The input file digits is 42,000 rows and 785 columns.
the first 10 entries in digits0 are:
head(digits.0,10)
[1] 2 5 6 18 24 55 64 70 99 109
Here is the rbind command (again):

digits.0data<- rbind(digits[digits.0,-1])

As i understand this the rbind should go to the first entry in digits.0 =2, and then use that as the reference in the input file digits and then populate digits.0data with the row =2 column data from the input file digits (skipping the first column).

Question, is R performing the row bind for every row that is populated in digits.0 without a loop?
Thus the digits.0data vector is 4132 rows and is a 784 columns per row?

Or since a loop is not specified, it is only performing the rbind command once and only reading row 2 of the input file to populate digits.0data with just the 784 columns values?

Thanks,

John2

Maybe I am missing something but rbind seems unnecessary in this instance. The work of selecting rows is being done by the square brackets, [ ]. You can see below that the result is the same with and without the rbind. And no loop is necessary. The length of the vector passed as the first argument within the square brackets determines the number of rows in the output.

DF <- data.frame(A = 11:15, B = 21:25, C = 31:35)
DF
#>    A  B  C
#> 1 11 21 31
#> 2 12 22 32
#> 3 13 23 33
#> 4 14 24 34
#> 5 15 25 35
DF[c(2,4,5), -1]
#>    B  C
#> 2 22 32
#> 4 24 34
#> 5 25 35
rbind(DF[c(2,4,5), -1])
#>    B  C
#> 2 22 32
#> 4 24 34
#> 5 25 35

Created on 2020-03-25 by the reprex package (v0.3.0)

Ok, thank you for confirming my belief that the rbind will perform the bind for each entry in digits.0.
Any your response " The length of the vector passed as the first argument within the square brackets determines the number of rows in the output" is succinct and clear.

There is a step towards the end of the assignment where we are to look at ways to improve the sample coding and I'll consider your input when I get to that step of the assignment.

John2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.