[R] how to export density_function output?

Dieter Menne dieter.menne at menne-biomed.de
Mon Nov 7 16:08:02 CET 2005


Alessandro Bigi <abigi <at> agrsci.unibo.it> writes:

> 
> Dear all,
> 
> quite a naive question: I have a data frame and I computed "the kernel 
> density estimate" with density on each column.
> Now I'd like to export in a txt file the density function output for each 
> column, but, when if I use write.table, I get a message "Error in 
> as.data.frame.default(x[[i]], optional = TRUE) : can't coerce class 
> "density" into a data.frame"

dens = density(c(-20,rep(0,98),20))
str(dens) # If in doubt, write str()
#List of 7
# $ x        : num [1:512] -23.1 -23.0 -22.9 -22.8 -22.7 ...
# $ y        : num [1:512] 4.46e-05 5.77e-05 7.40e-05 9.45e-05 1.20e-04 ...
# $ bw       : num 1.02
# $ n        : int 100
# $ call     : language density.default(x = c(-20, rep(0, 98), 20))
# $ data.name: chr "c(-20, rep(0, 98), 20)"
# $ has.na   : logi FALSE
# - attr(*, "class")= chr "density"
# You probably need x and y
write.table(cbind(dens$x,dens$y),file="dens.txt")




More information about the R-help mailing list