[R] read floats from file into array

leo mueller llug.dan at googlemail.com
Tue Jul 21 13:57:50 CEST 2009


big thanks to all, the x[[1]] worked fine! :)

2009/7/21 Duncan Murdoch <murdoch at stats.uwo.ca>:
> On 21/07/2009 6:09 AM, leo mueller wrote:
>>
>> hi all,
>>
>> i have a simple question. instead of defining my measurements in a
>> static way like ...
>>
>> x <- c(-0.475, -1.553, -0.434, -1.019, 0.395)
>>
>> ... i'd like them to be read from a file ...
>>
>> x <- read.table("07a673ac0cb1f7f8fa293860566f633c/1/raw0.txt",
>> header=FALSE)
>> d1 <- density(x, kernel = "gaussian")
>>
>> with a formatting that looks like:
>>
>> 4.2840000000e-01
>> 6.7583333333e-01
>> 8.2920000000e-01
>> 7.8566666667e-01
>> 6.6336666667e-01
>> 5.4080000000e-01
>> 4.7283333333e-01
>> 4.3770000000e-01
>> 4.3743333333e-01
>> 4.1026666667e-01
>> 3.6283333333e-01
>> 3.2770000000e-01
>> 4.9096666667e-01
>> [...]
>>
>> R quits and says:
>>
>>> d1 <- density(x, kernel = "gaussian")
>>
>> Error in density.default(x, kernel = "gaussian") :
>>  argument 'x' must be numeric
>> Calls: density -> density.default
>> Execution halted
>>
>> is there any possibility to convert this / make this work?
>
> read.table returns a dataframe, i.e. a list of vectors.  density wants a
> vector.  So you will probably get what you want using
>
> d1 <- density(x[[1]], kernel="gaussian")
>
> You can use names(x) to find the name of the 1st column for a nicer syntax;
> it is probably V1 (for "variable 1"), so you could do
>
> y <- x$V1
> density(y, ...)
>
> Duncan Murdoch
>




More information about the R-help mailing list