Ben
Ben

Reputation: 1522

What is -c and nrow doing in this function?

What is the purpose of -c and nrow in this function?

bdf <- by(bdf, bdf$Serial_number, function(SN, k) {
                     SN[-c(1:k, (nrow(SN)-k+1):nrow(SN)),]
                     }, k = 10)

by() splits the data frame bdf by the second argument Serial_number and applys the function function(SN, k) in the third argument. I don't understand the body of the function.

Upvotes: 0

Views: 74

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 146050

c() creates a vector. - makes the numbers in the vector negative. The vector is in the "row" position of [, so it is omitting the rows from 1 to k, and from nrow(SN) - k + 1 to the end of the data frame. So it's chopping off the first k and last k - 1 rows of the data frame.

Upvotes: 5

Related Questions