Antoni Parellada
Antoni Parellada

Reputation: 4801

Function sweep() in R error message

With these data:

set.seed(1); n = 50; x1 = rnorm(n, 10, 3); x2 = rnorm(n, 15, 3); x3 = rnorm(n, 20, 3)
dataframe = data.frame(x1,x2,x3)

I wanted to reproduce the centering (without scaling) achieved with:

centered = scale(dataframe, scale=F)

using the sweep function. But I get this error message:

sweep(dataframe, 2, mean, "-")
Error in as.vector(x, mode) : 
  cannot coerce type 'closure' to vector of type 'any'

I have tried changing the data frame format to a matrix without success with the same error message.

What am I misunderstanding?

Upvotes: 2

Views: 692

Answers (1)

akrun
akrun

Reputation: 887531

The mean is a function name and by using that in the sweep along with - implies there are two functions. However, according to ?sweep, the usage is

sweep(x, MARGIN, STATS, FUN = "-", check.margin = TRUE, ...)

So, the mean could be possibly the "STATS". It could be a object name of a vector of column means (though not clear from the OP's post). If we provide the "STATS" as the column mean (colMeans(dataframe)), it will do the subtraction (-) of each column of 'dataframe' by the corresponding value from the vector of column means i.e.

res <- sweep(dataframe, 2, colMeans(dataframe), "-")
head(res, 2)
#         x1         x2         x3
#1 -2.1807063  0.8423383 -1.4036437
#2  0.2495851 -2.1880585  0.5838039

head(centered, 2)
#            x1         x2         x3
#[1,] -2.1807063  0.8423383 -1.4036437
#[2,]  0.2495851 -2.1880585  0.5838039

Here is another example that shows how the sweep works by specifying the MARGIN as 2. We create a vector ('v1') with length equal to the number of columns of the 'data.frame' ('d1')

 v1 <- 1:3
 d1 <- data.frame(Col1 = 1:5, Col2= 6:10, Col3 = 11:15)
 sweep(d1, 2, v1, "-")
 #  Col1 Col2 Col3
 #1    0    4    8
 #2    1    5    9
 #3    2    6   10
 #4    3    7   11
 #5    4    8   12

It does the - between the first column of 'd1' with corresponding element of 'v1', 2nd column with 2nd element of 'v1' and so on.. However, it cannot take another function to do some operation on the same dataset and return that as "STATS" vector to perform the FUN between the dataset and transformed one unless we do that explicitly as shown in the solution earlier.

sweep(d1, 2, mean, "-")
#Error in as.vector(x, mode) : 
#cannot coerce type 'closure' to vector of type 'any'

data

dataframe <- data.frame(x1, x2, x3)

Upvotes: 3

Related Questions