[R] noob requesting help

Rui Barradas ruipbarradas at sapo.pt
Thu Jun 14 15:45:53 CEST 2012


Hello,

Thanks, I wasn't really liking it very much but it more of a diffuse 
feeling than of a founded thought. It's good to see an example.

Rui Barradas

Em 14-06-2012 14:22, David Winsemius escreveu:
>
> On Jun 14, 2012, at 3:20 AM, Rui Barradas wrote:
>
>> Hello,
>>
>> Now the output of str() says 'dat' is a list not a data.frame. That's
>> why R is complaining about dimensions (lack of, in this case).
>>
>> Try
>>
>> dat2 <- data.frame(do.call(cbind, dat), stringsAsFactors=FALSE)
>
> The construction data.frame(cbind(.)) should be severely deprecated. It
> coerces all the columns to be of the same  class and removes all the
> attributes except names. This is what happens to a POSIXlt "vector":
>
> data.frame(do.call(cbind,  list(a=1:10, b=as.POSIXlt(ISOdate(2001, 1:10,
> 1))) ),stringsAsFactors=FALSE )
>      a                                                b
> 1   1                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0
> 2   2                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0
> 3   3           12, 12, 12, 12, 12, 12, 12, 12, 12, 12
> 4   4                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1
> 5   5                     0, 1, 2, 3, 4, 5, 6, 7, 8, 9
> 6   6 101, 101, 101, 101, 101, 101, 101, 101, 101, 101
> 7   7                     1, 4, 4, 0, 2, 5, 0, 3, 6, 1
> 8   8      0, 31, 59, 90, 120, 151, 181, 212, 243, 273
> 9   9                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0
> 10 10                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0
>
>
> Use instead:
>
> dat2 <- data.frame( dat, stringsAsFactors=FALSE)
>
> The data.frame function will do the cbinding actions but will preserve
> column attributes such as POSIXlt. The process may convert to POSIXct
> from POSIXlt.
>
>  > structure(data.frame(list(a=1:10, b=as.POSIXlt(ISOdate(2001, 1:10,
> 1)))) )
>      a                   b
> 1   1 2001-01-01 12:00:00
> 2   2 2001-02-01 12:00:00
> 3   3 2001-03-01 12:00:00
> 4   4 2001-04-01 12:00:00
> 5   5 2001-05-01 12:00:00
> 6   6 2001-06-01 12:00:00
> 7   7 2001-07-01 12:00:00
> 8   8 2001-08-01 12:00:00
> 9   9 2001-09-01 12:00:00
> 10 10 2001-10-01 12:00:00
>  > str(data.frame(list(a=1:10, b=as.POSIXlt(ISOdate(2001, 1:10, 1)))) )
> 'data.frame':    10 obs. of  2 variables:
>   $ a: int  1 2 3 4 5 6 7 8 9 10
>   $ b: POSIXct, format: "2001-01-01 12:00:00" "2001-02-01 12:00:00"
> "2001-03-01 12:00:00" ...
>



More information about the R-help mailing list