{"id":3330,"date":"2020-03-16T16:47:49","date_gmt":"2020-03-16T23:47:49","guid":{"rendered":"http:\/\/blogs.oregonstate.edu\/gemmlab\/?p=3330"},"modified":"2020-09-16T16:43:32","modified_gmt":"2020-09-16T23:43:32","slug":"coding-stories-tips-and-tricks","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/gemmlab\/2020\/03\/16\/coding-stories-tips-and-tricks\/","title":{"rendered":"Coding stories, tips, and tricks"},"content":{"rendered":"\n<p><a href=\"https:\/\/mmi.oregonstate.edu\/people\/clara-bird\">Clara\nBird<\/a><sup>1<\/sup> and <a href=\"https:\/\/mmi.oregonstate.edu\/people\/karen-lohman\">Karen Lohman<\/a><sup>2<\/sup><\/p>\n\n\n\n<p class=\"has-small-font-size\"><sup>1<\/sup>Masters Student in Wildlife Science, Geospatial\nEcology of Marine Megafauna Lab<\/p>\n\n\n\n<p class=\"has-small-font-size\"><sup>2<\/sup>Masters Student in Wildlife Science, Cetacean\nConservation and Genomics Laboratory <\/p>\n\n\n\n<p>In a departure from my typical science-focused blog, this week I thought I would share more about myself. This week I was inspired by International\u2019s Woman\u2019s Day and, with some reflection on the last eight months as a graduate student, I decided to look back on the role that coding has played in my life. We hear about how much coding can be empowering but I thought it might be cool to talk about my personal experience of feeling empowered by coding. I\u2019ve also invited a fellow grad student in the Marine Mammal Institute, <a href=\"https:\/\/mmi.oregonstate.edu\/people\/karen-lohman\">Karen Lohman<\/a>, to co-author this post. We\u2019re going to briefly talk about our experience with coding and then finish with advice for getting started with coding and coding for data analysis.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Our Stories<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Clara<\/strong><\/h3>\n\n\n\n<p>I\u2019ve only been coding for a little over two and a half years. In summer 2017 I did an NSF REU (Research Experience for Undergraduates) at Bigelow Laboratory for Ocean Sciences and for my project I taught myself python (with the support of a post-doc) for a data analysis project. During those 10 weeks, I coded all day, every workday. From that experience, I not only acquired the hard skill of programming, but I gained a good amount of confidence in myself, and here\u2019s why: For the first three years of my undergraduate career coding was a daunting skill that I knew I would eventually need but did not know where to start. So, I essentially ended up learning by jumping off the deep end. I found the immersion experience to be the most effective learning method for me. With coding, you find out if you got something right (or wrong) almost instantaneously. I\u2019ve found that this is a double-edged sword. It means that you can easily have days where everything goes wrong. But, the feeling when it finally works is what I think of when I hear the term empowerment. I\u2019m not quite sure how to put it into words, but it&#8217;s a combination of independence, confidence, and success.\u00a0 <\/p>\n\n\n\n<p>Aside from learning the fundamentals, I finished that summer with confidence in my ability to teach myself not just new coding skills, but other skills as well. I think that feeling confident in my ability to learn something new has been the most helpful aspect to allow me to hit the ground running in grad school and also keeping the \u2018imposter syndrome\u2019 at bay (most of the time).<\/p>\n\n\n\n<p><strong>Clara\u2019s Favorite Command: <\/strong>pd.groupby (python) \u2013 Say you have a column of measurements and a second column with the field site of each location. If you wanted the mean of the measurement per each location, you could use groupby to get this. It would look like this: dataframe.groupby(\u2018Location\u2019)[\u2018Measurement\u2019].mean().reset_index()<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Karen<\/strong><\/h3>\n\n\n\n<p>I\u2019m quite new to coding, but once I\nstarted learning I was completely enchanted! I was first introduced to coding\nwhile working as a field assistant for a PhD student (a true R wizard who has\nsince developed deep learning computer vision packages for automated camera\ntrap image analysis) in the cloud forest of the Ecuadorian Andes. This remote\njungle was where I first saw how useful coding can be for data management and\nanalysis. It was a strange juxtaposition between being fully immersed in nature\nfor remote field work and learning to think along the lines of coding syntax.\nIt wasn\u2019t the typical introduction to R most people have, but it was an\neffective hook. We were able to produce preliminary figures and analysis as we\ncollected data, which made a tough field season more rewarding. Coding gave us\ninstant results and motivation.<\/p>\n\n\n\n<p>I\ncommitted to fully learning how to code during my first year of graduate\nschool. I first learned linux\/command line and python, and then I started\nworking in R that following summer. My graduate research uses population\ngenetics\/genomics to better understand the migratory connections of humpback\nwhales. This research means I spend a great deal of time working to develop\nbioinformatics and big data skills, an essential skill for this area of\nresearch and a goal for my career. For me, coding is a skill that only returns\nwhat you put in; you can learn to code quite quickly, if you devote the time.\nAfter a year of intense learning and struggle, I am writing better code every\nday. <\/p>\n\n\n\n<p>In\ngrad school research progress can be nebulous, but for me coding has become a\nconcrete way to measure success. If my code ran, I have a win for the week. If\nnot, then I have a clear place to start working the next day. These \u201ctiny wins\u201d\nare adding up, and coding has become a huge confidence boost.<\/p>\n\n\n\n<p><strong>Karen\u2019s Favorite Command: <\/strong>grep (linux) &#8211; Searches for a string pattern and prints all lines containing a match to the screen. Grep has a variety of flags making this a versatile command I use every time I\u2019m working in linux.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advice<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Getting Started<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Be kind to yourself, think of it as a foreign language. It takes a long time and a lot of practice.<\/li><li>Once you know the fundamental concepts in any language, learning another will be easier (we promise!).<\/li><li>Ask for help! The chances that you have run into a unique error are quite small, someone out there has already solved your problem, whether it\u2019s a lab mate or another researcher you find on Google! <\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Coding Tips<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. Set yourself up\nfor success by formatting your datasheets properly<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>Instead of making your spreadsheet easy to read, try and think about how you want to use the data in the analysis.<\/li><li>Avoid formatting (merged cells, wrap text) and spaces in headers<\/li><li>Try to think ahead when formatting your spreadsheet <ul><li>Maybe chat with someone who has experience and get their advice!<\/li><\/ul><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. Start with a plan, start on paper<\/h4>\n\n\n\n<p>This low-tech solution saves countless hours of code confusion. It can be especially helpful when manipulating large data frames or in multistep analysis. Drawing out the structure of your data and checking it frequently in your code (with \u2018head\u2019 in R\/linux) after manipulation can keep you on track. It is easy to code yourself into circles when you don\u2019t have a clear understanding of what you\u2019re trying to do in each step. Or worse, you could end up with code that runs, but doesn\u2019t conduct the analysis you intended, or needed to do. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. Good organization and habits will get you far<\/h4>\n\n\n\n<p>There is an excellent blog by <a href=\"https:\/\/nicercode.github.io\/blog\/2013-04-05-projects\/\">Nice R Code<\/a> on project organization and file structure. I highly recommend reading and implementing their self-contained scripting suggestions. The further you get into your data analysis the more object, directory, and function names you have to remember. Develop a naming scheme that makes sense for your project (i.e. flexible, number based, etc.) and stick with it. Temporary object names in functions or code blocks can be a good way to clarify what is the code-in-progress or the code result.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"974\" height=\"195\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/2115\/files\/2020\/03\/Picture1.png\" alt=\"\" class=\"wp-image-3331\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/2115\/files\/2020\/03\/Picture1.png 974w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/2115\/files\/2020\/03\/Picture1-300x60.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/2115\/files\/2020\/03\/Picture1-768x154.png 768w\" sizes=\"auto, (max-width: 974px) 100vw, 974px\" \/><figcaption>Figure 1. An example of project based workflow directory organization from Nice R Code (<a href=\"https:\/\/nicercode.github.io\/blog\/2013-04-05-projects\/\">https:\/\/nicercode.github.io\/blog\/2013-04-05-projects\/<\/a> )<\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">4. Annotate. Then annotate some more.<\/h4>\n\n\n\n<p>Make comments in your code so you can remember what each section or line is for. This makes debugging much easier! Annotation is also a good way to stay on track as you code, because you\u2019ll be describing the goal of every line (remember tip 1?). If you\u2019re following a tutorial (or STACKoverflow answer), copy the web address into your annotation so you can find it later. At the end of a coding session, make a quick note of your thought process so it\u2019s easier to pick up when you come back. It\u2019s also a good habit to add some \u2018metadata\u2019 details to the top of your script describing what the script is intended for, what the input files are, the expected outputs, and any other pertinent details for that script. Your future self will thank you!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"274\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/2115\/files\/2020\/03\/Screen-Shot-2020-03-15-at-10.28.23-AM-1024x274.png\" alt=\"\" class=\"wp-image-3332\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/2115\/files\/2020\/03\/Screen-Shot-2020-03-15-at-10.28.23-AM-1024x274.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/2115\/files\/2020\/03\/Screen-Shot-2020-03-15-at-10.28.23-AM-300x80.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/2115\/files\/2020\/03\/Screen-Shot-2020-03-15-at-10.28.23-AM-768x206.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/2115\/files\/2020\/03\/Screen-Shot-2020-03-15-at-10.28.23-AM-1536x412.png 1536w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/2115\/files\/2020\/03\/Screen-Shot-2020-03-15-at-10.28.23-AM.png 1634w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Figure 2. Example code with comments explaining the purpose of each line. <\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">5. Get with git\/github already<\/h4>\n\n\n\n<p>Github is a great way to manage version control. Remember how life-changing the advent of dropbox was? This is like that, but for code! It\u2019s also become a great open-source repository for newly developed code and packages. In addition to backing up and storing your code, GitHub has become a \u2018coding CV\u2019 that other researchers look to when hiring. <\/p>\n\n\n\n<p>Wondering how to get started with GitHub? Check out this guide:  https:\/\/guides.github.com\/activities\/hello-world\/ <\/p>\n\n\n\n<p>Looking for a good text\/code editor? Check out atom (<a href=\"https:\/\/atom.io\/\">https:\/\/atom.io\/<\/a>), you can push your edits straight to git from here.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">6. You don&#8217;t have to learn everything, but you should probably learn the R Tidyverse ASAP<\/h4>\n\n\n\n<p>Tidyverse is a collection of data manipulation packages that make data wrangling a breeze. It also includes ggplot, an incredibly versatile data visualization package. For python users hesitant to start working in R, Tidyverse is a great place to start. The syntax will feel more familiar to python, and it has wonderful documentation online. It\u2019s also similar to the awk\/sed tools from linux, as dplyr removes any need to write loops. Loops in any language are awful, learn how to do them, and then how to avoid them. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">7. Functions!<\/h4>\n\n\n\n<p>Break your code out into blocks that can be run as functions! This allows easier repetition of data analysis, in a more readable format. If you need to call your functions across multiple scripts, put them all into one \u2018function.R\u2019 script and source them in your working scripts. This approach ensures that all the scripts can access the same function, without copy and pasting it into multiple scripts. Then if you edit the function, it is changed in one place and passed to all dependent scripts.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">8. Don&#8217;t take error messages personally<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>Repeat after me: Everyone googles for every other line of code, everyone forgets the command some (&#8230;.er every) time.<\/li><li>Debugging is a lifestyle, not a task item.<\/li><li>One way to make it less painful is to keep a list of fixes that you find yourself needing multiple times. And ask for help when you\u2019re stuck! <\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">9. Troubleshooting<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>Know that you\u2019re supposed to google but not sure what?<ul><li>start by copying and pasting the error message<\/li><\/ul><\/li><li>When I started it was hard to know how to phrase what I wanted, these might be some common terms<ul><li>A <strong>dataframe <\/strong>is the coding equivalent of a spreadsheet\/table<\/li><\/ul><ul><li>Do you want to combine two dataframes side by side? That\u2019s a <strong>merge<\/strong><\/li><\/ul><ul><li>Do you want to stack one dataframe on top of another? That\u2019s <strong>concatenating<\/strong><\/li><\/ul><ul><li>Do you want to get the average (or some other statistic) of values in a column that are all from one group or category? Check out <strong>group by <\/strong>or <strong>aggregate<\/strong><\/li><\/ul><ul><li>A <strong>loop <\/strong>is when you loop through every value in a column or list and do something with it (use it in an equation, use it in an if\/else statement, etc).<\/li><\/ul><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Favorite Coding Resource (other than github\u2026.)<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Learnxinyminutes.com<ul><li>This is great \u2018one stop googling\u2019 for coding in almost any language! I frequently switch between coding languages, and as a result almost always have this open to check syntax. <\/li><\/ul><\/li><li><a href=\"https:\/\/swirlstats.com\/\">https:\/\/swirlstats.com\/<\/a><ul><li>This is a really good resource for getting an introduction to R<\/li><\/ul><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Parting Thoughts<\/h3>\n\n\n\n<p>We hope that our stories and advice have been helpful! Like many skills, you tend to only see people once they have made it over the learning curve. But as you\u2019ve read Karen and I both started recently and felt intimidated at the beginning. So, be patient, be kind to yourself, believe in yourself, and good luck!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Clara Bird1 and Karen Lohman2 1Masters Student in Wildlife Science, Geospatial Ecology of Marine Megafauna Lab 2Masters Student in Wildlife Science, Cetacean Conservation and Genomics Laboratory In a departure from my typical science-focused blog, this week I thought I would share more about myself. This week I was inspired by International\u2019s Woman\u2019s Day and, with &hellip; <a href=\"https:\/\/blogs.oregonstate.edu\/gemmlab\/2020\/03\/16\/coding-stories-tips-and-tricks\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Coding stories, tips, and tricks<\/span><\/a><\/p>\n","protected":false},"author":9938,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[188686,1310686,1],"tags":[831,1310532,135655,1310685,482,1310598,712761],"class_list":["post-3330","post","type-post","status-publish","format-standard","hentry","category-current-projects","category-behavior-and-body-condition","category-uncategorized","tag-advice","tag-clara-bird","tag-coding","tag-gray-whale-individual-behavior-and-body-condition","tag-outreach","tag-python","tag-r"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/gemmlab\/wp-json\/wp\/v2\/posts\/3330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/gemmlab\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/gemmlab\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/gemmlab\/wp-json\/wp\/v2\/users\/9938"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/gemmlab\/wp-json\/wp\/v2\/comments?post=3330"}],"version-history":[{"count":1,"href":"https:\/\/blogs.oregonstate.edu\/gemmlab\/wp-json\/wp\/v2\/posts\/3330\/revisions"}],"predecessor-version":[{"id":3333,"href":"https:\/\/blogs.oregonstate.edu\/gemmlab\/wp-json\/wp\/v2\/posts\/3330\/revisions\/3333"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/gemmlab\/wp-json\/wp\/v2\/media?parent=3330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/gemmlab\/wp-json\/wp\/v2\/categories?post=3330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/gemmlab\/wp-json\/wp\/v2\/tags?post=3330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}