157k views
2 votes
How do I create a variable in R Studio and have it sort data?

The professor gave an assignment that basically expects us all to be pros at this language. I'm not a programmer and this is not a programming class, it's accounting. Whatsmore, the professor has yet to tell us how to do anything except calculate a mean. Somehow we are just expected to learn this.
**The manuals and videos do not help me. I need someone to explain something. I'm not even sure where to start with figuring this out. **
Here's what we need to do: import data from Excel and then classify it into categories based on ranges of numbers. All of the data is a number from 1 to 6. We are supposed to put it into three categories (1 and 2 in one, 3 and 4 in another, 5 and 6 in another). These are results from a survey. I know how to get the spreadsheet in, but how do I do this? I need to somehow create a variable and then have it sort data into categories.

1 Answer

4 votes

Final answer:

To sort data into categories in R Studio, import the Excel data, then use the mutate() and case_when() functions from the dplyr package to create a new variable that classifies the data into categories based on specified ranges.

Step-by-step explanation:

To create a variable in R Studio that sorts data into categories, you would first import your Excel data using a function such as readxl::read_excel(). Assuming your data is now in a data frame, you can use the mutate() and case_when() functions from the dplyr package to create a new variable that categorizes your data based on the specified ranges. Here's an example code snippet:

# Assuming your data frame is named df
library(dplyr)
df <- df %>%
mutate(category = case_when(
your_variable %in% 1:2 ~ 'Category 1',
your_variable %in% 3:4 ~ 'Category 2',
your_variable %in% 5:6 ~ 'Category 3'
))

This code will create a new column in your data frame, category, where each number from 1 to 6 is classified into 'Category 1', 'Category 2', or 'Category 3'. Replace your_variable with the actual name of your imported data column containing the numbers 1 to 6.

Remember that in descriptive statistics, organizing and summarizing data is imperative, and this is a practical example of turning numerical variables into categorical variables. Such transformation helps in better understanding the distribution and characteristics of survey results or other data types.

User Alex Chumbley
by
8.1k points