简体   繁体   中英

error in density.default : x must be numeric

This question might have been repeated. But even after going through the previous links, I am not able to solve this. I have a file as follows:

data <- read.table("data.txt", header=TRUE)

             Samp1           Samp2     Samp3
cg00000029  0.79015390399987 0.8301816 0.8966661
cg00000108 0.970260858767027 0.9655997 0.9699428
cg00000109 0.948456317952246 0.9209855 0.9325146
cg00000165 0.267769194351135 0.2370634 0.3867273

I wish to create a density plot out of the column (say Samp1). When I use the following

>plot(density(na.omit(data$Samp1)), col="black")

I get the following error:

Error in density.default(na.omit(data$Samp1)) : argument 'x' must be numeric

Can anyone help me know how to i correct this? I have created density plots for similar files, but did not get this error. It is only for this file.

Your help appreciated. Thanks in advance..

Well, for some reason your data is non-numeric: have you tried using as.numeric() to coerce it into the right type?

Edit : Using unlist() to convert it out of a list type seems to be the answer

I was getting the same problem, I was able to solve it by doing:

a<- density(treeDF$real)
plot(a$x, a$y, col="turquoise2")
b<- density(treeDF$rpart)
lines(b$x, b$y, col="deeppink3")
c<- density(treeDF$rpart_Fourier)
lines(c$x, c$y, col="blue")
legend("topright", legend = c("real value", "rpart()", "rpart() + Fourier tem"),
    col=c("turquoise2", "deeppink3", "blue"), lty=1:1, cex=0.8, box.lty = 0)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM