Function sweep() in R error message

Blank

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?

akrun

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python numpy or pandas equivalent of the R function sweep()

From Dev

how to use STATS in sweep function of R

From Dev

R function length error message

From Dev

apply a function from different list in R using sweep function

From Dev

Error message ".innerHTML is not a function"

From Dev

Trap Error message in a function

From Dev

Suppress error message in R

From Dev

Suppress error message in R

From Dev

Modal analysis with input sweep to obtain a transfer function

From Dev

Modal analysis with input sweep to obtain a transfer function

From Dev

Expected function / variable error message

From Dev

TkInter grid function error message

From Dev

Error: message.$delete is not a function

From Dev

Interpreting error message in regression in R

From Dev

R Metrics auc() error message

From Dev

If Else Statement in R Error Message

From Dev

R starts up with error message

From Dev

FluentValidation modify error message in Must function

From Dev

How to add error message to this php function?

From Dev

Simple Error Message no matching function for call to

From Dev

dplyr - error message after applying function

From Dev

Error message: Undefined reference to 'print' function

From Dev

Error message: Undefined reference to 'print' function

From Dev

Receiving error message when initialising JavaScript function

From Dev

dplyr - error message after applying function

From Dev

Bash function that returns value and prints error message

From Dev

Why I get error message that onVideoSelect is not a function?

From Dev

Inner Error: Message: controllerFactory.createForCurrentScope is not a function

From Dev

Firebase Cloud Function not showing error message

Related Related

HotTag

Archive