[R] Data.frames : difference between x$a and x[, "a"] ? - How set new values on x$a with a as variable ?

Bert Gunter gunter.berton at gene.com
Fri Sep 10 17:20:24 CEST 2010


Well, let's see if the following helps or just adds to the confusion.

First lists are vectors of mode "list" . But they are general
recursive structures (in fact, completely general).

Second, data frames are lists: each "column" of a data frame is a
component (member) of the list with the additional requirement that
all the components must be the same length. The reason for the scare
quotes areound "column" will become clear shortly.

Now, for examples:

> x <- data.frame(a=1:3,b=list(c=4:6))
> x
  a c
1 1 4
2 2 5
3 3 6

## This is as documented in ?data.frame.


## Now compare:

> y <- data.frame(a=1:3)
> y$b <- list(c=4:6)
> y
  a       b
1 1 4, 5, 6
2 2 4, 5, 6
3 3 4, 5, 6

## A different result that one might think should be the same as the
previous one. What's going on is: y is a data frame, so all components
must have the same length. Because the length of b, a list, is just 1,
it is replicated to be the proper length:

> y$b
$c
[1] 4 5 6

$c
[1] 4 5 6

$c
[1] 4 5 6

##The b component is still a list:

> mode(y$b)
[1] "list"

## of course:

> y$c=7:9
> y
  a       b c
1 1 4, 5, 6 7
2 2 4, 5, 6 8
3 3 4, 5, 6 9

## This is correct, since the "c" component is a vector of length 3. Note also:

> mode(y[,3])
[1] "numeric"
> mode(y[[3]])
[1] "numeric"
> mode(y[3])
[1] "list"

## All these are correct = agree with documented behavior of "[" and
"[[" because a data.frame IS a list.

Cheers,
Bert










On Fri, Sep 10, 2010 at 7:13 AM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Sep 10, 2010, at 9:42 AM, Hadley Wickham wrote:
>
>>>>> I'm having trouble parsing this. What exactly do you want to do?
>>>>
>>>> 1 - Put a list as an element of a data.frame. That's quite convenient
>>>> for my pricing function.
>>>
>>> I think this is a really bad idea. data.frames are not meant to be
>>> used in this way. Why not use a list of lists?
>>
>> It can be very convenient, but I suspect the original poster is
>> confused about the different between vectors and lists.
>
> I wouldn't be surprised if someone were confused, since my reading of some
> (but not all) of the help documents has led me to think that lists _were_
> vectors, just not vectors of atomic mode. And one oft-illustrated method for
> creating a list is:  alist <- vector(mode="list", length=10). I am perhaps
> less confused than I was two years ago but my confusion about all the
> possible permutations of mode, typeof, expression, formula, and class and
> the extraction methods therefrom definitely persists. I think the authors of
> the documentation are of divided opinion or usage on this topic.
>
> Best;
> David.
>
>
>>
>> Hadley
>>
>> --
>> Assistant Professor / Dobelman Family Junior Chair
>> Department of Statistics / Rice University
>> http://had.co.nz/
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list