## Scaling data in R gives spurious Error "length of 'center' must equal the number of columns of 'x'"

scale multiple columns in r

how to create a scale in r

r scaling methods

scaling dataset in r

scale function in r z score

scales in r

I am trying to scale a data.frame in the range of 0 and 1 using the following code:

for(i in 1:nrow(data)) { x <- data[i, ] data[i, ] <- scale(x, min(x), max(x)-min(x)) } Data: x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 15 6 6 0 9 3 1 4 5 1 1 13 0 0 20 5 28 2 24 14 7 0 15 7 0 11 3 3 4 15 7 0 30 0 344 3 10 5 2 0 6 2 0 5 0 0 2 7 1 0 11 0 399 4 9 4 2 0 5 2 0 4 0 0 2 6 1 0 10 0 28 5 6 2 1 0 3 1 0 2 0 0 1 3 1 0 6 0 82 6 9 4 2 0 5 2 0 4 0 0 2 6 1 0 10 0 42

But I am getting the following error message:

Error in scale.default(x, min(x), max(x) - min(x)) (from #4) : length of 'center' must equal the number of columns of 'x'

Using this data , your example works for me:

data <- matrix(sample(1:1000,17*6), ncol=17,nrow=6) for(i in 1:nrow(data)){ x <- data[i, ] data[i, ] <- scale(x, min(x), max(x)-min(x)) }

Here another option using scale , without a loop. You need just to provide a `scale`

and a `center`

with same columns that your matrix.

maxs <- apply(data, 2, max) mins <- apply(data, 2, min) scale(data, center = mins, scale = maxs - mins)

**EDIT** how to access the result.

The scale returns a matrix with 2 attributes. To get a data.frame, you need just to coerce the scale result to a data.frame.

dat.scale <- scale(data, center = mins, scale = maxs - mins) dat.sacle <- as.data.frame(dat.scale)

**Understanding `scale` in R,** =FALSE) , it will only subtract the mean but not divide by the std deviation.) In the life sciences, many assays measure only the relative abundances of components in each sample. Such data, called compositional data, require special treatment to avoid misleading conclusions.

**Using the standardize package - CRAN,** with a mean of 0 and a standard deviation of 1, you can use the scale function from the base package which is a generic function whose default method centers and/or scales the columns of a numeric matrix. This article represents concepts around the need to normalize or scale the numeric data and code samples in R programming language which could be used to normalize or scale the data. Please feel free to comment/suggest if I missed mentioning one or more important points. Also, sorry for the typos. Following are the two different ways which

there is also another way to scale the data by creating a function

data_norm<- function(x) {((x-min(x))/(max(x)-min(x)))} variables_norm<- as.data.frame(lapply(data[1:17], data_norm)) summary(variables_norm)

**Standardize data columns in R,** Data can be mean-centered in R in several ways, and you can even write Perhaps the most simple, quick and direct way to mean-center your data is by using the function scale() . If you give sweep() a value (i.e. a summary statistic) and a function, fake data X = matrix(runif(2000), 100, 20) # test them Scaling data in R, resulting in error: “length of 'center' must equal the number of columns of 'x'”

**6 ways of mean-centering data in R,** Description Do most of the painful data preparation for a data science project with a mini- Depends R (>= 3.3.0), lubridate, stringr, Matrix, progress scales <- build_scales(adult, cols = "auto", verbose = TRUE) Function to identify dates columns and give there format. Doesnt generate any mistake (but is a bit slower). Invert the effect of the scale function. This function can be used to un-scale a set of values. This unscaling is done with the scaling information "hidden" on a scaled data set that should also be provided. This information is stored as an attribute by the function scale() when applied to a data frame.

**[PDF] Package 'dataPreparation',** Specifying both rate and scale to [dpqr]gamma is a warning (if they are essentially the merge() works in more cases where the data frames include matrices. aov() and manova() gave spurious warning about singular error model in the Using the scale function. Perhaps the most simple, quick and direct way to mean-center your data is by using the function scale(). By default, this function will standardize the data (mean zero, unit variance). To indicate that we just want to subtract the mean, we need to turn off the argument scale = FALSE.

**New features in this version,** scale is generic function whose default method centers and/or scales the columns of a numeric matrix. Stan Young, an activist against spurious statistics, and I found a genetics-based study claiming significance from statistical data even in the reputable New England Journal of Medicine – where

##### Comments

- Thanks. But when I assign the output of scale function to some variable then I am unable extract the data portion. I think there are several other attributes in the output from scale function.
- @Shahzad your data is a matrix with 2 extra attributes. You can for example , use
`as.vector(data)`

to remove them. - @Shahzad are they really in the way? You can use
`attributes(x) <- list(dim=dim(x))`

- @MatthewPlourde yes. that 's why I said 'for example' .Thanks for your help. You suggestion is the way if you want to keep a matrix with the same dimensions.
- @agstudy. Sorry. But, I am still unable to access the data using $ notation such as data$x1 gives $ operator is invalid for atomic vectors.