4

I want to get pvalues from a data set. I have not had any problems to use pnorm, but I have now.

data(iris)

iris[,-5]<- scale(as.matrix(iris[,-5]))

# K-Means Cluster Analysis
fit <- kmeans(iris[,-5], 5) # 5 cluster solution
# get cluster means 
aggregate(iris[,-5],by=list(fit$cluster),FUN=mean)
# append cluster assignment
mydata <- data.frame(iris, fit$cluster)

pval<- pnorm(iris[,-5])

After this, I get message of "Error in pnorm(q, mean, sd, lower.tail, log.p) : Non-numeric argument to mathematical function".

What is the problem? I do not understand why this is happening.

Please let me know.

1 Answers1

6

You are trying to pass a dataframe to a function that is requesting a numeric vector:

> is.numeric(iris[,-5])
[1] FALSE
> str(iris[,-5])
'data.frame':   150 obs. of  4 variables:
 $ Sepal.Length: num  -0.898 -1.139 -1.381 -1.501 -1.018 ...
 $ Sepal.Width : num  1.0156 -0.1315 0.3273 0.0979 1.245 ...
 $ Petal.Length: num  -1.34 -1.34 -1.39 -1.28 -1.34 ...
 $ Petal.Width : num  -1.31 -1.31 -1.31 -1.31 -1.31 ...

Try passing just a single column, like:

pnorm(iris[,1])
Thomas
  • 43,637
  • 12
  • 109
  • 140
  • It is possible to make pvalues by yours, but I do not understand. Why does the method I wrote above do not work? Anyway, thank you for your answer. –  Dec 12 '13 at 04:05
  • 1
    @user3027252 Functions generally accept only certain types of objects as arguments. Take a look at `?pnorm`. You have to pass a numeric vector, not a dataframe. You could do `sapply(iris[,-5], pnorm)` to get values for each column of your dataframe, for example. – Thomas Dec 12 '13 at 08:04
  • I have used `pnorm` before, and at that time I succeeded to use `pnorm`. Maybe I accidently changed the dataframe to matrix, but it had column names just like data frame. Maybe there must be some other methods to use pnorm in dataframe, by transforming the dataframe to matrix. Anyway, I am just a novice in R, and I appreciate your help! Good day! –  Dec 13 '13 at 01:15