Reputation: 21
I have a code I'm working with which has the following line,
data2 <- apply(data1[,-c(1:(index-1))],2,log)
I understand that this creates a new data frame, from the data1, taking column-wise values log-transformed and some columns are eliminated, but I don't understand how the columns are removed. what does 1:(index-1) do exactly?
Upvotes: 1
Views: 93
Reputation: 263382
The ":" operator creates an integer sequence. Because (1:(index-1) )
is numeric and being used in the second position for the extraction operator"[" applied to a dataframe, it is is referring to column numbers. The person writing the code didn't need the c
-function. It could have been more economically written:
data1[,-(1:(index-1))]
# but the outer "("...")"'s are needed so it starts at 1 rather than -1
So it removes the first index-1
columns from the object passed to apply
. (As MrFlick points out, index must have been defined before this gets passed to R. There's not default value or interpretation for index
in R.
Upvotes: 2
Reputation: 887251
Suppose the index is 5, then index -1 returns 4 so the sequence will be from 1 to 4 i.e. and then we use -
implies loop over the columns other than the first 4 columns as MARGIN = 2
Upvotes: 0