简体   繁体   中英

R plot density not numeric

Hello I'm very new to R but am wishing to plot a simple probability density function (PDF) from a data frame which has two columns, Tag and DistFromMean . The data frame contains

str(TaggleTest)
'data.frame':   36452 obs. of  2 variables:
 $ Tag         : num  2997 2997 2997 2997 2997 ...
 $ DistFromMean: num  19.24 36.36 13.07 9.27 26.1 ...

As listed, both columns are numeric. The Tag column contains 12 different tag numbers. What I wish to do is plot a PDF for each tag. From what I have read the command should look like this:

plot(density(TaggleTest$Tag=="2997"))

Each time I run the command it returns:

Error in density.default(TaggleTest$Tag == "2997") : argument 'x' must be numeric

Would someone be able to tell me where I am going wrong. Regards Don

You're giving a logical argument zu density in your example. What you want to do is feed the part of "DistFromMean" to density whose "Tag" is equal to 2997. This works as follows:

TaggleTest <- data.frame(Tag=c(2997,2997,2997,2997,2997),                   
                         DisFromMean=c(19.24,36.36,13.07,9.27,26.1))

plot(density(TaggleTest[ TaggleTest$Tag==2997, "DisFromMean" ]))

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