0

I would like to calculate the following command:

scale.data<-as.matrix((datamatrix-apply(datamatrix,1,mean))/apply(datamatrix,1,sd))

However return this error:

Error in apply(datamatrix, 1, mean): dim(X) must have a positive length Traceback:

  1. apply(datamatrix, 1, mean)
  2. stop("dim(X) must have a positive length")
Peter
  • 11,500
  • 5
  • 21
  • 31
ASN
  • 1
  • Welcome to stack overflow. The inclusion of a reproducible example would make it easier to help. [Link for guidance on asking questions](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Peter Jun 20 '23 at 07:18
  • please provide a reproducible example of datamatrix, using dput(datamatrix) and paste the data in your question – ThomasIsCoding Jun 20 '23 at 08:29

1 Answers1

0

I think you missed the transpose operator t(), and your code is supposed to be

scale.data<-as.matrix((datamatrix-t(apply(datamatrix,1,mean))/apply(datamatrix,1,sd)))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • @SeforaNaomiAgrò then, please provide a reproducible example of `datamatrix`, using `dput(datamatrix)` and paste the data in your question – ThomasIsCoding Jun 20 '23 at 08:26
  • It is not good because return the same error. apply(datamatrix, 1, mean) -> dim(X) must have a positive length I try to calculate the mean in another way, like mean(datamatrix), but I didn't get a result. – ASN Jun 20 '23 at 08:28