R is a powerful programming language used for data analysis and statistical computing. Here is a quick reference guide to get you started with R programming.
Basic Syntax:
- Comments start with the “#” symbol
- Assignment operator is “<-“
- Function calls use parentheses, e.g. mean(x)
- “print()” function can be used to display results
- Use “?” before a function to get help, e.g. ?mean
Data Types:
- Numeric: numbers with decimal places, e.g. 3.14
- Integer: whole numbers, e.g. 5
- Character: text, e.g. “hello”
- Factor: categorical data, e.g. “male” or “female”
- Logical: binary values, either TRUE or FALSE
Vectors:
- A vector is a collection of values with the same data type
- Creation of vectors using c(), e.g. c(1,2,3)
- Use “[]” to access elements of a vector, e.g. x[2]
- Use “length()” to get the number of elements in a vector
Matrices:
- A matrix is a 2-dimensional vector with rows and columns
- Creation of matrices using matrix(), e.g. matrix(1:9, ncol=3)
- Use “[row, col]” to access elements of a matrix, e.g. m[2,3]
- Use “dim()” to get the dimensions of a matrix
DataFrames:
- A data frame is a 2-dimensional data structure with rows and columns
- Creation of data frames using data.frame(), e.g. data.frame(x=1:5, y=6:10)
- Use “$” to access columns of a data frame, e.g. df$x
- Use “nrow()” and “ncol()” to get the number of rows and columns
Reading Data:
- Use read.csv() to read csv files, e.g. read.csv(“data.csv”)
- Use read.table() to read other types of files, e.g. read.table(“data.txt”, sep=”\t”)
Data Manipulation:
- Use “head()” and “tail()” to view the first and last few rows of a data frame
- Use “subset()” to extract a subset of a data frame based on conditions, e.g. subset(df, x > 3)
- Use “merge()” to combine two data frames based on common columns
Plotting:
- Use “plot()” to create basic plots, e.g. plot(x, y)
- Use “hist()” to create histograms, e.g. hist(x)
- Use “boxplot()” to create box plots, e.g. boxplot(x)
- Use “barplot()” to create bar plots, e.g. barplot(x)
Statistics:
- Use “mean()” to calculate the mean of a vector, e.g. mean(x)
- Use “median()” to calculate the median of a vector, e.g. median(x)
- Use “sd()” to calculate the standard deviation of a vector, e.g. sd(x)
- Use “summary()” to get a summary of a data frame, e.g. summary(df)
Comments are closed.