R Coding in Stats iQ
About R Coding in Stats iQ
R is a statistical programming language that is widely used for flexible and powerful analysis. This page explains how you can start coding in R from right inside Stats iQ.
Selecting Dataframe Variables for R Code
Before you can begin coding in R, you will need to select the variables that will be part of your “dataframe.” Your dataframe is the collection of variables that you would like to perform your analysis on. These variables can be any type.
- On the left, select variables you’d like to analyze with R.
Qtip: You can select multiple variables, but limits vary based on the size of your data. See Dataframe Size below for more details. - Click Advanced.
- Select R Code.
Once this is done, your next step will be to name your variables.
Dataframe Size Guidelines
There are some limitations to the size of the dataframe you can use when coding with R in Stats iQ. Depending on the types of variables you include in your dataframe, it is possible that you may be able to analyze a larger number of variables or a smaller number of variables based on the size of a single cell.
By “cell,” we mean to visualize the data as a table or spreadsheet. The number of cells in your dataset is the number of rows (individual data points / responses) multiplied by the number of columns (the fields in your dataset). The size of each individual cell (i.e., the amount of memory it takes up) is dependent on the variable type.
The maximum dataframe size is 512MB. Note that the dataframe does not comprise the entire dataset, but only selected variables.
Naming Dataframe Variables for R Code
Once you’ve chosen your variables and clicked R Code, you will be asked to name your variables. This section covers how to give your variables R code-compatible names.
- Under Variable code compatible name, enter a code-compatible name for each variable. Each name must meet the following requirements:
- Must use letters, numbers, periods ( . ), and underscores ( _ ) only.
- Must not start with a number, underscore, or a period followed by a number.
- Must not be a reserved R word, such as TRUE or else.
- Must not be a duplicate of an existing code-compatible name within the same R code card.
- When you click into a field to type a name, these requirements will appear to the right. You will see how many requirements the name has met (indicated by a green checkmark) or failed to meet (indicated by a red minus sign).
- Once you’re finished, click Confirm.
Qtip: You will not be able to confirm until all names are code-compatible.
Modifying Dataframe Variables in the R Code Card
After you choose your first dataframe variables, you can change or rename them as needed.
- Click on the Dataframe Variables button in the top-right of the R code card’s header.
- To edit the name of an existing variable, make edits under the Variable code compatible name column.
Qtip: If you’re editing variable names, make sure to meet the requirements. - To change the dataset variable itself, select a different variable under the Variable name column.
Example: We added NPS group, but want to change this to the numeric NPS value (0-10) instead.
- To remove a variable, click the minus sign ( – ).
- To add another variable, click Add variable, then choose something from the dropdown. You will then have to name this variable.
- Once you’re finished, click Save changes.
Writing and Executing R Code
The R Code card is split in half down the middle. The left half is where you can edit your code. The right half is where you will see the outputs from executing your code.
You can execute a single line of code by clicking the Run Line button at the top or using the keyboard shortcuts Cmd + Enter (for Mac) or Ctrl + Enter (for PC) when your cursor is on a specific line.
Once you trigger the execution of a line, you will see the output associated with the line in the Output tab of the right half of the card. The Output tab will display the line of code that was executed in blue and the result of the execution right underneath.
Autocomplete for Dataframe Variables
You will see autocomplete suggestions for your dataframe variables if you type df$ in the editor.
Using R Packages
R coding in Stats iQ comes pre-installed with hundreds of the most popular R packages used for analysis. Click on the Packages tab in the right half of the card to see the list of available packages. You can use the search bar at the top to search for specific packages.
To use a package, use the library() command in the code editor to the left. Enter the package name in the parentheses and then execute the code.
Requesting New Packages
You can request that R code packages be added to Stats iQ if you cannot find them in the list of available packages. When searching in the Packages tab, if no matches are found, you will see an option to request new packages.
Clicking the Request Package button will take you to a quick survey where you can enter details about the packages you would like to utilize.
Downloading Generated Plots and Images
Any plots or images that you generate with your code can be downloaded very easily. Simply scroll to the specific plot or image and right click on the image to see copy or save options.
Incompatible R Commands
The view command is incompatible with R Code Cards.