Programming with R for Official Statistics: Introduction to Programming (2024)

Last updated on 2024-07-09 | Edit this page

Overview

Questions

  • What is programming?
  • What is object oriented programming?
  • How to document code?
  • What is a directory?

What is programming?

Programmers use programming languages to give instructions to theircomputers. In this course, we will learn how to use the open sourcelanguage R to complete common tasks required in the field of officialstatistics. This includes the basics of R, data manipulation, and bestpractices.

There are a few reasons why programming with R is useful for officialstatistics. Data manipulation and analysis with R is:

  • Time-saving: R can complete many computations on a large amountof data that would take a person a long time manually

  • Reproducible: This code can be re-run with other data with smallmodifications and shared with others to be applied to other newpurposes

  • Transparent: When you’ve completed a script using best practices,you should be left with a clear list of instructions to complete thedata analysis in the form of code. This avoids “black boxes” where ananalyst is unsure what they’ve done to the data to get it to it’s finalform

R is an object oriented programming language

Object oriented programming languages use objects as theirmain tools. These objects have classes, which describe theirgeneral properties. For example, in R you might work withnumeric objects, which would contain numbers. You could alsowork with characters, which would be composed of text. We’llexplore classes and data types thoroughly in Episode 3 (Data Types andStructures). We can assign “labels” to these objects, creating avariable and use them interchangeably. We assign objects withan assignment operator. In R, the most commonly used assignment operatoris <-. Try reproducing the example below on your machineby entering the code into the console and hitting the “run” button.

R

# Assign a number to a variablenumber_flowers <- 8# Print the variable's contentsprint(number_flowers)

We can get the value stored within the variable by printing it.

[1] 8

Assigning a new value to a variable breaks the connection with theold value; R forgets that number and applies the variable name to thenew value.

When you assign a value to a variable, R only stores the value, notthe calculation you used to create it. This is an important point ifyou’re used to the way a spreadsheet program automatically updateslinked cells. Let’s look at an example.

# Reassign the variablenumber_flowers <- 7

{: .language-r)

OUTPUT

[1] 7

Variable Naming Conventions

Historically, R programmers have used a variety of conventions fornaming variables. The . > character in R can be a validpart of a variable name; thus the above assignment could have easilybeen weight.kg <- 57.5. This is often confusing to Rnewcomers who have programmed in languages where . has amore significant meaning. Today, most R programmers 1) start variablenames with lower case letters, 2) separate words > in variable nameswith underscores, and 3) use only lowercase letters, underscores, andnumbers in variable names. The Tidyverse Style Guide includes asection on thisand other style considerations.

Documenting Code

Notice that in the above examples, hashtags (#) are usedbefore giving instructions that are intended for you rather than R.Hashtags produce comments, which are handy for leavinginformation about the code that will follow. Commenting as much code aspossible is part of best practices. Always comment your code! You owe itto your colleagues who may see your code (not to mention your futurecoding self).

# Hashtags go before commented code, which is not run# print("This code will not be run")print("Always comment your code!")

OUTPUT

[1] "Always comment your code!"

Directories

A directory is a location on your machine. Say you’d like to open afile that’s located in a folder on your computer. We need to tell Rwhere to look for the file if we expect to find it. Directories areusually listed by referencing nested folders separated by slashes. Thereare small differences due to operating system (OS), so refer todocumentation specific to your OS when learning to work with folderstructures.

For example: /Users/Documents/Learning-R points to afolder called “Learning-R” in a user’s documents folder. Depending onyour IDE (Integrated Development Environment) and setup, you can printyour current directory, known as the working directory. Rautomatically reads and writes files from and to your current workingdirectory.

R

# print current working directory getwd()

OUTPUT

[1] "/Users/Documents/

Before beginning our lessons, please set your working directory tothe folder that we created in the setup section withsetwd(). For example, if your folder is namedLearning-R:

R

# change current working directory setwd("~/Documents/Learning-R")

Key Points

  • Programming makes our work faster, more reproducible, and moretransparent.
  • R is an object oriented programming language
  • Document your code with comments
  • A working directory is the active location on your computer where Rcan read and write files
Programming with R for Official Statistics: Introduction to Programming (2024)

FAQs

Can I start programming with R? ›

Start coding using RStudio.

cloud Primers offer a cloud-based learning environment that will teach you the basics of R all from the comfort of your browser. RStudio. cloud doesn't require you to install any software on your computer, making it easy to dip your toe into data science with R with a minimum of fuss.

Is R programming hard? ›

R is considered one of the more difficult programming languages to learn due to how different its syntax is from other languages like Python and its extensive set of commands. It takes most learners without prior coding experience roughly four to six weeks to learn R. Of course, this depends on several factors.

Is using R considered programming? ›

R is a programming language and a software environment for statistical computing and graphics.

Can R be used for statistics? ›

R is a free, open source statistical programming language. It is useful for data cleaning, analysis, and visualization. It complements workflows that require the use of other software. You can read more about the language and find documentation on the R Project Website.

Top Articles
Www Craigslist Sacramento Ca
Jackson County Jail, IN Inmate Search: Roster & Mugshots
Skylar Vox Bra Size
Danielle Moodie-Mills Net Worth
Weeminuche Smoke Signal
Jennifer Hart Facebook
Get train & bus departures - Android
Crossed Eyes (Strabismus): Symptoms, Causes, and Diagnosis
Meg 2: The Trench Showtimes Near Phoenix Theatres Laurel Park
Boat Jumping Female Otezla Commercial Actress
Erin Kate Dolan Twitter
Ap Chem Unit 8 Progress Check Mcq
83600 Block Of 11Th Street East Palmdale Ca
Gmail Psu
Guilford County | NCpedia
Jesus Calling Oct 27
Conan Exiles Colored Crystal
Kiddle Encyclopedia
Urban Airship Expands its Mobile Platform to Transform Customer Communications
How pharmacies can help
Account Suspended
Ruse For Crashing Family Reunions Crossword
Best Mechanics Near You - Brake Masters Auto Repair Shops
Keci News
Boscov's Bus Trips
Pocono Recird Obits
Craigs List Tallahassee
BJ 이름 찾는다 꼭 도와줘라 | 짤방 | 일베저장소
What Equals 16
Impact-Messung für bessere Ergebnisse « impact investing magazin
Jayme's Upscale Resale Abilene Photos
55Th And Kedzie Elite Staffing
Trinket Of Advanced Weaponry
Lindy Kendra Scott Obituary
Cavanaugh Photography Coupon Code
Frequently Asked Questions - Hy-Vee PERKS
Grays Anatomy Wiki
Solve 100000div3= | Microsoft Math Solver
Appleton Post Crescent Today's Obituaries
Old Peterbilt For Sale Craigslist
Unlock The Secrets Of "Skip The Game" Greensboro North Carolina
Vanessa West Tripod Jeffrey Dahmer
The disadvantages of patient portals
Lyca Shop Near Me
Anya Banerjee Feet
Luvsquad-Links
Tricia Vacanti Obituary
Thotsbook Com
Bustednewspaper.com Rockbridge County Va
Wisconsin Volleyball titt*es
Pas Bcbs Prefix
Where Is Darla-Jean Stanton Now
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 6252

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.