# Export data to R

## 🌐 Load data into RStudio

Begin by exporting the respondents' data from NGSurvey in CSV format

<figure><img src="/files/3J73Z1fUnNXrUUEMO9E4" alt=""><figcaption></figcaption></figure>

In the RStudio use the script to import the CSV file and set up your dataset.

```r
survey_csv <- read.csv(file="C:/Users/Admin/Downloads/ClientSatisfactionSurvey.csv",stringsAsFactors = TRUE, header=TRUE, sep=",")
```

Where:

* **survey\_csv**: Variable to store the data.
* **read.csv**: Function to read CSV file.
* **file**: Path to the CSV file.
* **stringsAsFactors**: Convert string columns to factor.
* **header**: Use the first row as column names.
* **sep**: Delimiter used in the file (comma).

## 📊 Analyzing and Visualizing data in RStudio

Now that your data is loaded into R or RStudio, you can start analyzing it and creating visualizations and tables.

1. **Data Summary:** Provides a quick summary of the dataset, including statistics for each column.

```r
summary(survey_csv)
```

Where:

* **summary**: Function to provide basic descriptive statistics for each column in your dataset
* **survey\_csv**: Your dataset.

2. **Simple table:** For this walk through we have chosen the variable Recommend\_likelihood that represents the survey question "How likely are you to recommend our service to a friend or colleague"

```r
table(survey_csv$Recommend_likelihood
```

<figure><img src="/files/FB9S2gM3E6HzN01ASnw2" alt=""><figcaption></figcaption></figure>

Where:

* **table**: This function creates a frequency table.
* **survey\_csv$Recommend\_likelihood**: This specifies the column `Recommend_likelyhood` from the `survey_csv` dataset.
* **Output**: The frequency table shows how often each category of recommendation likelihood occurs in the dataset.

**Optional:** We recommend to ensure the correct ordering of the categorical variable in tables and visualizations.

```r
survey_csv$Recommend_likelihood <- factor(
  survey_csv$Recommend_likelihood,
  levels = c("Very unlikely", "Unlikely", 
  "Neutral", "Likely", "Very likely")
)
```

Where:

* **factor**: Converts the variable to a factor (categorical variable).
* **levels**: Specifies the order of categories for the factor.

3. **Visualization:** simple bar chart visualization of the recommendation likelihood distribution.

Create the **barplot** to visualize the data:

```r
barplot(
  table(survey_csv$Recommend_likelihood), 
  main = "How likely are you to recommend our service to a friend or colleague",
  xlab = "Recommendation Likelihood",
  ylab = "Frequency",
  col = "lightblue",
  las = 2,
  cex.names = 0.8  # Reduces the size of the bar labels
)
```

<figure><img src="/files/hOriYKDUEm7D82vfyiAr" alt=""><figcaption></figcaption></figure>

Where:

* **barplot**: Generates a bar chart.
* **table(survey\_csv$Recommend\_likelyhood)**: Creates a frequency table for the bar heights.
* **main**: Sets the title of the plot.
* **xlab**: Label for the x-axis.
* **ylab**: Label for the y-axis.
* **col**: Sets the bar color to light blue.
* **las = 2**: Rotates axis labels to be perpendicular for better readability.
* **cex.names = 0.8**: Reduces the size of the bar labels to prevent overlap.

**Optional:** Adjust margins and label positions for the barplot.

```r
par(mar = c(7, 5, 4, 2) + 0.1, mgp = c(4, 1, 0))
```

Where:

* **par**: Sets graphical parameters.
* **mar**: Adjusts plot margins: bottom, left, top, right (increased bottom margin to avoid overlap).
* **+ 0.1**: Slightly increases each plot margin to ensure extra space for plot elements, preventing them from being clipped or overcrowded.
* **mgp**: Controls the placement of axis title, labels, and line. Here, `4` shifts the x-axis label further down.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ngsurvey.com/walkthroughs/export-data-to-r.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
