Ben Ben - 3 years ago 98
R Question

What is -c and nrow doing in body of function

Can you please explain me what

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

is doing?
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download