Statistical Analysis for Thesis with R or Python
Introduction to R and Python
Your Toolkit: R and Python
When you work with data, you need the right tools. For statistical analysis, two programming languages stand out: R and Python. Both are powerful, free, and widely used by analysts and scientists.
R was built by statisticians, specifically for statistical analysis and data visualization. It has a massive ecosystem of packages for nearly any statistical task you can imagine. Python, on the other hand, is a general-purpose language that has become a data science powerhouse thanks to its simple syntax and powerful libraries like pandas and NumPy.
Think of R as a specialized workshop for statistics, while Python is a versatile, multi-purpose factory that’s also excellent at building statistical tools.
Getting Started with R
The best way to use R is with RStudio, an integrated development environment (IDE) that makes coding in R much easier. It gives you a console to run commands, an editor to write scripts, and windows to view plots, variables, and help files.
At its core, R can be used as a powerful calculator. You can type expressions directly into the RStudio console and press Enter.
# Basic arithmetic in R
5 + 3 # Addition
10 - 4 # Subtraction
6 * 7 # Multiplication
20 / 5 # Division
Beyond simple math, R organizes data into structures. The most common are vectors, lists, and data frames.
A vector is a sequence of elements of the same type, like numbers or text.
# A numeric vector
numeric_vector <- c(10, 20, 30, 40)
# A character vector
character_vector <- c("apple", "banana", "cherry")
A list is more flexible. It can contain elements of different types, including other vectors or even other lists.
# A list with different data types
my_list <- list(name = "Alex", age = 25, scores = c(88, 92, 95))
The most important data structure for statistical analysis is the data frame. It’s a table, like a spreadsheet, where columns are variables and rows are observations. Each column must have the same data type, but different columns can have different types.
# Creating a data frame
students <- data.frame(
name = c("Alex", "Beth", "Charles"),
age = c(25, 27, 26),
major = c("Statistics", "Biology", "Statistics")
)
# Print the data frame
print(students)
To run a script in RStudio, you write your commands in the editor (the top-left window), save the file (e.g., my_script.R), and then click the 'Source' button or run lines individually.
Getting Started with Python
Python is known for its readability. For data analysis, it's often used within Jupyter Notebooks. These are interactive documents that let you mix code, text, and visualizations in one place, making it perfect for exploring data.
Like R, Python can handle basic math right out of the box.
# Basic arithmetic in Python
5 + 3 # Addition
10 - 4 # Subtraction
6 * 7 # Multiplication
20 / 5 # Division
Python's fundamental data structures include lists and dictionaries.
A list is an ordered collection of items, which can be of any type. They are created with square brackets [].
# A list of numbers
scores = [88, 92, 95]
# A list of strings
names = ["Alex", "Beth", "Charles"]
A dictionary stores data in key-value pairs. They are great for data that has a natural label.
# A dictionary representing a person
person = {"name": "Alex", "age": 25, "major": "Statistics"}
For tabular data, the Python community relies on the pandas library. Its primary data structure is the DataFrame, which is very similar to R's data frame.
# First, import the pandas library
import pandas as pd
# Create a dictionary of data
data = {
'name': ['Alex', 'Beth', 'Charles'],
'age': [25, 27, 26],
'major': ['Statistics', 'Biology', 'Statistics']
}
# Create a DataFrame from the dictionary
students_df = pd.DataFrame(data)
# Print the DataFrame
print(students_df)
In a Jupyter Notebook, you write code in cells. You can run each cell individually to see the output immediately below it, which makes for a very interactive and exploratory workflow.
Key Differences at a Glance
While both languages can achieve the same goals in data analysis, their syntax and core data structures have some differences. Here’s a quick comparison.
| Feature | R | Python |
|---|---|---|
| Assignment | <- or = | = |
| Basic Sequence | Vector c() | List [] |
| Key-Value Store | Named List list() | Dictionary {} |
| Table Structure | data.frame() | pandas DataFrame() |
| Primary IDE | RStudio | Jupyter Notebook |
What is the primary distinction between the original design philosophies of R and Python?
In R, what is the most important and common data structure for storing tabular data, similar to a spreadsheet?
Getting comfortable with these basics is the first step. Once you can create and manipulate these data structures, you're ready to start loading, cleaning, and analyzing real-world data.