Basic Functions and Commands

STA1506 - Basic Statistical Computing · Statistical Software

Basic Functions and Commands

Statistical software is essential for managing and analysing data. This topic will focus on basic functions and commands that you will use frequently in your statistical computing tasks.

Understanding Basic Commands

Basic commands in statistical software allow you to perform fundamental operations such as data entry, data manipulation, and statistical calculations. Different software may have different syntax, but the concepts are similar. Here, we will focus on common commands that are often found in statistical software like R, Python, and SPSS.

Data Entry

Data entry is the first step in any data analysis process. You can enter data manually or import it from external files such as CSV or Excel files. Here is how you can do this in R:

data <- read.csv("datafile.csv")

In this command:

  • data is the name of the variable that will store your dataset.
  • read.csv is the function that reads a CSV file.
  • "datafile.csv" is the path to your CSV file.

Remember: Always check that your file path is correct to avoid errors.

Data Manipulation

Once your data is loaded, you may need to manipulate it. Common tasks include filtering rows, selecting columns, and summarising data.

Filtering Rows

To filter rows based on certain conditions, you can use the subset function in R. For example:

filtered_data <- subset(data, Age > 30)

This command creates a new variable filtered_data that contains only the rows where the Age column has values greater than 30.

Watch out: Ensure that the column names in your dataset match what you use in your commands. Otherwise, you may encounter errors.

Selecting Columns

To select specific columns from your dataset, you can use the following command:

selected_columns <- data[c("Name", "Age")] 

This command creates a new variable selected_columns that includes only the Name and Age columns from the original dataset.

Summarising Data

To summarise data, you can use the summary function:

summary(data)

This command provides basic statistics for each column in your dataset, such as mean, median, and standard deviation.

Tip: Use the str function to check the structure of your dataset. This will help you understand the types of data you are working with.

Statistical Functions

Statistical functions allow you to perform calculations on your data. Common functions include mean, median, variance, and standard deviation.

Calculating the Mean

To calculate the mean of a specific column, use the mean function:

mean_age <- mean(data$Age)

This command calculates the mean of the Age column and stores it in the variable mean_age.

Calculating the Median

To find the median, you can use the median function:

median_age <- median(data$Age)

This command calculates the median of the Age column.

Calculating Variance and Standard Deviation

The variance and standard deviation can be calculated using the var and sd functions, respectively:

age_variance <- var(data$Age)
age_sd <- sd(data$Age)

These commands calculate the variance and standard deviation of the Age column.

Generating Reports

After performing your analysis, you may want to generate reports. Most statistical software has functions to export your results to various formats, such as PDF or Word documents.

Exporting Results

In R, you can save your summary statistics to a text file using the following command:

write.table(summary(data), file = "summary.txt")

This command creates a text file called summary.txt that contains the summary statistics of your dataset.

Tip: Always check the format of your output file to ensure it meets your needs.

Common Errors

When using basic functions and commands, students often encounter several common errors. Here are a few to watch out for:

  • Incorrect file paths when importing data.
  • Using incorrect column names in commands.
  • Forgetting to load necessary libraries or packages before using specific functions.

Watch out: Always read error messages carefully, as they often indicate what went wrong.

Summary

  • Data entry can be done manually or through file imports.
  • Basic commands include filtering rows, selecting columns, and summarising data.
  • Statistical functions allow you to calculate mean, median, variance, and standard deviation.
  • Reports can be generated and exported in various formats.

Check your understanding

  1. What command would you use to read a CSV file in R?
  2. How can you filter rows in a dataset based on a specific condition?
  3. What function would you use to calculate the standard deviation of a column?
  4. How can you export summary statistics to a text file?