[R] sort list

Joshua Wiley jwiley.psych at gmail.com
Tue Mar 13 13:29:38 CET 2012


On Tue, Mar 13, 2012 at 5:15 AM, sybil kennelly <sybilkennelly at gmail.com> wrote:
> Thanks Josh. I'm quite new, just wondering re:factor levels?
>
> In this example (shamelessly stolen from the internet):
>
> schtyp
>
> [1] 0 0 1 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 0
>
> schtyp.f <- factor(schtyp, labels = c("private", "public"))
>
> schtyp.f
>
> [1] private private public private private private public private public
> [10] private public public public public private private public public
>
> [19] public private
>
>
> Levels: private public
>
>
>
> in my data i have a table:
>
>         var1    var2     var3
> cell1    x       x         x
> cell2    x       x         x
> cell3    x       x         x
>
> cell4
>
> .
> .
> .
> .
> cell100
>
>
> and i have a subset of those cells that are interesting to me as a list of
> data
> list1 = ["cell1, "cell5",cell19", "cell50", "cell70"]
>
> is it possible to create (similar to above):
>
> schtyp.f <- factor(schtyp, labels = c("special", "normal"))

Sure.  Again, probably better to have cells of interest in a vector,
not a list a la:

list1 <- c("cell1, "cell5",cell19", "cell50", "cell70")

your_data$mycells <- factor(your_data$cells %in% list1, c("Special",
"NotSpecial"))

basically compares the cells to those in your list and returns
TRUE/FALSE, which is then converted to a factor, labeled, and stored.
If you are just starting, some background reading will help.  Here are
some suggestions:

1) Go here: http://www.burns-stat.com/pages/tutorials.html and read
the tutorials for R -- Beginning (this should not take more than 1
day).
2) Sit down and read:
http://cran.r-project.org/doc/manuals/R-intro.pdf through Appendix A
(for now you can probably skip the rest of the appendices).  That will
probably take another entire day or so.
3) Head back to Patrick Burn's website:
http://www.burns-stat.com/pages/tutorials.html and read the
intermediate guide, The R Inferno (1-3 days depending if you can read
for 8 hours straight or not)

Cheers,

Josh

>
> so that when i plot this data, i can color the items in list1 as one color
> (eg all the special cells are red), and the rest of the items as a second
> color (eg all the other cells are black/blue)?
>
>
> Syb
>
>
>
> On Tue, Mar 13, 2012 at 11:48 AM, Joshua Wiley <jwiley.psych at gmail.com>
> wrote:
>>
>> Hi Sybil,
>>
>> You cannot turn a list into a factor.  You could do:
>>
>> cell_data <-c('cell1','cell2')
>> factor_list <- factor(cell_data)
>>
>> or if you already have a list, unlist() or as.vector() may convert it
>> into a vector that you can then convert to a factor.
>>
>> Cheers,
>>
>> Josh
>>
>> On Tue, Mar 13, 2012 at 4:29 AM, sybil kennelly <sybilkennelly at gmail.com>
>> wrote:
>> > Hello can anyone help please?
>> >
>> > i read two words "cell1", "cell2" into a list. I want to turn this list
>> > into a factor.
>> >
>> >> cell_data <-list(c('cell1','cell2'))
>> >
>> >
>> >> cell_data
>> > [[1]]
>> > [1] "cell1" "cell2"
>> >
>> >
>> >
>> >> factor_list <- factor(cell_data)
>> > Error in sort.list(y) : 'x' must be atomic for 'sort.list'
>> > Have you called 'sort' on a list?
>> >
>> >
>> >
>> >> sort.list(cell_data)
>> > Error in sort.list(cell_data) : 'x' must be atomic for 'sort.list'
>> > Have you called 'sort' on a list?
>> >
>> >
>> > Can anyone explain?
>> >
>> > Syb
>> >
>> >        [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > 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.
>>
>>
>>
>> --
>> Joshua Wiley
>> Ph.D. Student, Health Psychology
>> Programmer Analyst II, Statistical Consulting Group
>> University of California, Los Angeles
>> https://joshuawiley.com/
>
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list