+ Reply to Thread
Page 1 of 27 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ... LastLast
Results 1 to 15 of 392

Thread: Today I Learned: ____

  1. #1
    Devorador de queso
    Points: 97,471, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent PosterActivity Award
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,984
    Thanks
    308
    Thanked 2,639 Times in 2,254 Posts

    Today I Learned: ____




    I thought it might be nice to just have a place to post some happy little things you learned or (re)discovered about R that makes your life nicer and you just want to share! Heck it can be just some nice things that you really appreciate or love about R that you just want to share. With that...

    TIL: The split function is nice! I was going through some old code and had a data table that had the form

    TIL: I don't know why I don't use the stringsAsFactors=F option when using read.table more often. Most times I just want to work with a string and dealing with factors can be a pain when you don't need to...

  2. The Following User Says Thank You to Dason For This Useful Post:

    trinker (01-03-2012)

  3. #2
    Probably A Mammal
    Points: 32,065, Level: 100
    Level completed: 0%, Points required for next Level: 0
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    2,567
    Thanks
    398
    Thanked 618 Times in 551 Posts

    Re: Today I Learned: ____

    I was looking up some programming stuff about R and ran across a discussion at SO about efficient "for loops" by not using for. In particular, this is when you're dealing with indexed loops (as opposed to loops over values like for (i in c("CA", "WA", "AZ")), say). You can simply do

    Code: 
    lapply(seq(10), ... stuff here ...)
    in place of

    Code: 
    for (i in 1:10) {... stuff here ...}
    It also has the benefit of not introducing the index variable into the environment, which I hate when I try to keep my environment clean in my scripts.

    @Dason, I rarely use stringsAsFactors because I usually specify colClasses. That way, I have direct control over what my classes are. Of course, if I want R to handle that and just keep things as numeric or character, stringsAsFactors is definitely the way to go.

  4. The Following User Says Thank You to bryangoodrich For This Useful Post:

    Dason (08-18-2012)

  5. #3
    Devorador de queso
    Points: 97,471, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent PosterActivity Award
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,984
    Thanks
    308
    Thanked 2,639 Times in 2,254 Posts

    Re: Today I Learned: ____

    Quote Originally Posted by bryangoodrich View Post
    I was looking up some programming stuff about R and ran across a discussion at SO about efficient "for loops" by not using for. In particular, this is when you're dealing with indexed loops (as opposed to loops over values like for (i in c("CA", "WA", "AZ")), say). You can simply do

    Code: 
    lapply(seq(10), ... stuff here ...)
    in place of

    Code: 
    for (i in 1:10) {... stuff here ...}
    I've used that trick before but wasn't sure how helpful it is. I should benchmark something and see what kind of gains are possible. But there do seem to be some things you might not be able to do with that trick as easily as you can do with a for loop.

    @Dason, I rarely use stringsAsFactors because I usually specify colClasses. That way, I have direct control over what my classes are. Of course, if I want R to handle that and just keep things as numeric or character, stringsAsFactors is definitely the way to go.
    I typically don't need too much control but do get annoyed when things are factors and I just want strings so stringsAsFactors is nice for me.

  6. #4
    ggplot2orBust
    Points: 72,900, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    4,424
    Thanks
    1,815
    Thanked 931 Times in 812 Posts

    Re: Today I Learned: ____

    ETIL (12:43 am): I have seen the colwise function in plyr before and thought "oh ok whatever" with no understanding of what it does or bothering to learn. Then I'm reading ggplot2 (Springer book by Wickham) and I see it actually takes a column function and generates a new function that operates on all columns in a dataframe. How awesome is that?

    Code: 
    library(plyr)
    
    median(mtcars)  #I don't work on dataframes : (
    median(mtcars$hp)  #just columns : ( x 2
    colwise(median)  #voodoo magic (see how it's generating a function)
    DFmedian <- colwise(median)  #Here's the creation of dataframe function (Insert Dason BWHAHAHA here)
    DFmedian(mtcars)  #Look ma I work on dataframes : )
    He has numcolwise and catcolwise to work on numeric or categorical columns only as well.
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  7. The Following User Says Thank You to trinker For This Useful Post:

    Dason (08-18-2012)

  8. #5
    Devorador de queso
    Points: 97,471, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent PosterActivity Award
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,984
    Thanks
    308
    Thanked 2,639 Times in 2,254 Posts

    Re: Today I Learned: ____

    It does a little bit more than just that. Otherwise using something like apply(mtcars, 2, median) would work just as well. But it does seem like it might provide a nice wrapper to stick into lapply if you have a bunch of dataframes you want to get colwise summaries for inside a list.

  9. #6
    ggplot2orBust
    Points: 72,900, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    4,424
    Thanks
    1,815
    Thanked 931 Times in 812 Posts

    Re: Today I Learned: ____

    Yeah it has added niceties over apply(df, 2, function) but my example was to just be minimal. The specific cat and num version is also nice. Up til now I've been using:
    Code: 
    NUM <- function(dataframe)dataframe[,sapply(dataframe,is.numeric)]
    apply(NUM(CO2), 2, median)
    It also works nicely with ddply as the function argument. I think this will save time for initial data examination.
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  10. The Following User Says Thank You to trinker For This Useful Post:

    Dason (08-18-2012)

  11. #7
    ggplot2orBust
    Points: 72,900, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    4,424
    Thanks
    1,815
    Thanked 931 Times in 812 Posts

    Re: Today I Learned: ____

    By the way nice idea Dason.
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  12. The Following User Says Thank You to trinker For This Useful Post:

    Dason (01-03-2012)

  13. #8
    ggplot2orBust
    Points: 72,900, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    4,424
    Thanks
    1,815
    Thanked 931 Times in 812 Posts

    Re: Today I Learned: ____

    TIL: You can choose every other of something using TRUE FALSE


    Code: 
    mtcars[c(T,F)]  #every odd column
    mtcars[c(F,T)]  #every even column
    mtcars[c(T,F), ]   #odd row
    mtcars[c(F,T), ]   #even row
    Not sure where I saw that one at:
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  14. The Following User Says Thank You to trinker For This Useful Post:

    bryangoodrich (01-04-2012)

  15. #9
    Devorador de queso
    Points: 97,471, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent PosterActivity Award
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,984
    Thanks
    308
    Thanked 2,639 Times in 2,254 Posts

    Re: Today I Learned: ____

    Seems like whoever posted that was most likely very handsome and probably at the moment sitting next to an adorable dog.

    That works for the same reason that something like mtcars[colnames(mtcars) == "cyl"] works.

    Code: 
    colnames(mtcars) == "cyl" # is just a logical vector
    # and you can index which columns or rows you want using a logical vector
    mtcars[colnames(mtcars) == "cyl"]
    
    # And R is smart in that it will recycle vectors to give you a vector
    # as long as you need
    mtcars[c(T,F)] # is really expanding out to mtcars[c(T,F,T,F,T,F,T,F,T,F,T)]

  16. #10
    Probably A Mammal
    Points: 32,065, Level: 100
    Level completed: 0%, Points required for next Level: 0
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    2,567
    Thanks
    398
    Thanked 618 Times in 551 Posts

    Re: Today I Learned: ____

    Ever since I used that recycling property in my "melt in base" function, I've been keen to notice its utility. I like that simple way of grabbing even/odd columns. I don't know when it will be useful, but it certainly shortens things up in the code. That creativity can go a long way!

  17. #11
    R purist
    Points: 35,103, Level: 100
    Level completed: 0%, Points required for next Level: 0
    TheEcologist's Avatar
    Location
    United States
    Posts
    1,921
    Thanks
    303
    Thanked 608 Times in 341 Posts

    Re: Today I Learned: ____

    Quote Originally Posted by Dason View Post
    TIL: I don't know why I don't use the stringsAsFactors=F option when using read.table more often. Most times I just want to work with a string and dealing with factors can be a pain when you don't need to...
    You know that is something you can change on start-up (Rprofile).

    See ?options

    I for instance have custom entries @ $defaultPackages, $help.try.all.packages, $digits, $editor, $pdfviewer

    You could change $stringsAsFactors to False if you need to
    The true ideals of great philosophies always seem to get lost somewhere along the road..

  18. #12
    Devorador de queso
    Points: 97,471, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent PosterActivity Award
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,984
    Thanks
    308
    Thanked 2,639 Times in 2,254 Posts

    Re: Today I Learned: ____

    I work with too many different computers that I prefer not modifying my Rprofile. I think on one of my computers I have R display a fortune on load up but I don't want to become dependent on having anything preset for me. Although I've made an exception in the past to have it always select Iowa State as my CRAN repository but that doesn't really change how I use R or how R handles anything.

  19. #13
    R purist
    Points: 35,103, Level: 100
    Level completed: 0%, Points required for next Level: 0
    TheEcologist's Avatar
    Location
    United States
    Posts
    1,921
    Thanks
    303
    Thanked 608 Times in 341 Posts

    Re: Today I Learned: ____

    Quote Originally Posted by Dason View Post
    I work with too many different computers that I prefer not modifying my Rprofile. I think on one of my computers I have R display a fortune on load up but I don't want to become dependent on having anything preset for me. Although I've made an exception in the past to have it always select Iowa State as my CRAN repository but that doesn't really change how I use R or how R handles anything.
    This little line of code solved the multiple PC problem for me.

    Code: 
    if(Sys.info()[1]=="Linux") {setwd("/home/ggplothater/Dropbox")} else
                              {setwd("D:/Dropbox/My Dropbox")}
    Place you customations in the that folder and you should be fine. Then all you need to change is that piece of code in the Rprofile when you install a new copy of R on a new machine (and source in all your customations). Likely won't work on a system that wont let you install Dropbox though.

    Don't know if it will be useful for you, but it works for me.. got R just the way I want it no matter where I am.
    The true ideals of great philosophies always seem to get lost somewhere along the road..

  20. #14
    Devorador de queso
    Points: 97,471, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent PosterActivity Award
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,984
    Thanks
    308
    Thanked 2,639 Times in 2,254 Posts

    Re: Today I Learned: ____

    I definitely thought of doing something like that but unfortunately about half the computers I use don't allow dropbox. Which I've been trying to get changed because it would make a lot of other things easier for me. But then again I'm alright with loading R vanilla every time.

    And please tell me your linux username isn't actually ggplothater.

  21. #15
    Devorador de queso
    Points: 97,471, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent PosterActivity Award
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,984
    Thanks
    308
    Thanked 2,639 Times in 2,254 Posts

    Re: Today I Learned: ____


    Quote Originally Posted by halfnormal View Post
    (tiptoeing in)
    a thread like this would be awesome in the education forum for all of us students....

    (tiptoeing out...)
    It probably would be a good thread for there. I don't have anything relevant to post there at the moment though but you should feel free to start that thread yourself.

  22. The Following User Says Thank You to Dason For This Useful Post:


+ Reply to Thread
Page 1 of 27 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ... LastLast

           




Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats