# Export data to R

## 🌐 Load data into RStudio

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

<figure><img src="https://1025048312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8fLhS0bmfBRyq0HdUm%2Fuploads%2FyftK49SGJvh7fsYo5Pg4%2Fimage.png?alt=media&#x26;token=2bbc0413-b6cc-4319-9e26-4b44373d3180" 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="https://1025048312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8fLhS0bmfBRyq0HdUm%2Fuploads%2FOh6XfSr4TwCx5xArT4Gx%2Fimage.png?alt=media&#x26;token=a71d2ba7-9506-44ee-8eab-08b3088d2fdc" 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="https://1025048312-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8fLhS0bmfBRyq0HdUm%2Fuploads%2F6gtLMsHtRWQyWwOXRNOB%2Fimage.png?alt=media&#x26;token=9a1a35f1-720d-4b6c-ba5a-9ba127426666" 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.
