[R] How to do indexing after splitting my data-frame?

jim holtman jholtman at gmail.com
Sun Dec 21 00:55:35 CET 2008


'str' is your friend when trying to understand what the difference is:

> x <- data.frame(a=1:10, b=sample(1:2, 10, TRUE))
> x.s <- split(x, x$b)
>
> x.s
$`1`
    a b
1   1 1
2   2 1
5   5 1
10 10 1

$`2`
  a b
3 3 2
4 4 2
6 6 2
7 7 2
8 8 2
9 9 2

> str(x.s['1'])
List of 1
 $ 1:'data.frame':      4 obs. of  2 variables:
  ..$ a: int [1:4] 1 2 5 10
  ..$ b: int [1:4] 1 1 1 1
> str(x.s[['1']])
'data.frame':   4 obs. of  2 variables:
 $ a: int  1 2 5 10
 $ b: int  1 1 1 1
> x.s['1']$a
NULL
> x.s[['1']]$a
[1]  1  2  5 10
>

Notice that x.s['1'] returns a 'list', while x.s[['1']] returns a
dataframe which is probably what you were expecting.


On Sat, Dec 20, 2008 at 4:10 PM, Oliver Bandel
<oliver at first.in-berlin.de> wrote:
> Hello Jim,
>
>
> jim holtman <jholtman <at> gmail.com> writes:
>
>>
>> try:
>>
>> myindex <- "01-Dec-2008"
>>
>> weblog_by_date[[myindex]]$host
> [...]
>
>
> Ooops yes, that works!
>
> Thank you!
>
> I don't know why this works, and the other thing not.
>
> I thought the difference between [] and [[]] is just,
> that the one gives back a list and the other grabs out the
> values of a list by "unlist"ing it... so that one gets a vector instead of a
> complete list.
>
>
> Ciao,
>   oliver
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list