# R Basics

## Basic Objects <a href="#basic_objects" id="basic_objects"></a>

### Vectors (Classes) <a href="#vectors" id="vectors"></a>

| Type      | Example         |
| --------- | --------------- |
| Logical   | TRUE, FALSE     |
| Numeric   | 1, 55, 999      |
| Integer   | 1L, 32L, 0L     |
| Complex   | 2 + 3i          |
| Character | "great", "23.4" |

Creating a vector & print statement example

```r
apple <- c('red','green','yellow')

print(apple)

# Get the class of the vector
print(class(apple))
```

Output:

```r
"red"  "green"  "yellow"
"character"
```

### Anchors

<table><thead><tr><th>Anchor</th><th>Special Character</th><th>Example</th><th data-hidden></th></tr></thead><tbody><tr><td>Beginning of line</td><td>^</td><td>^New</td><td></td></tr><tr><td>End of line</td><td>\\$</td><td>y$</td><td></td></tr><tr><td>Beginning of string</td><td>\\A</td><td>\\AHello</td><td></td></tr><tr><td>End of string</td><td>\\Z or \\z</td><td>End\\Z</td><td></td></tr></tbody></table>

### Lists <a href="#lists" id="lists"></a>

Contain many different types of elements inside

```r
# Create a list.
list1 <- list(c(2,5,3),21.3)

# Print the list.
print(list1)
```

Output:

```r
[[1]]
2 5 3
[[2]]
21.3
```

### Matrices <a href="#matrices" id="matrices"></a>

Two-dimensional rectangular data set

```r
M = matrix( c('a','a','b','b','c','c'), nrow = 2, ncol = 3, byrow = TRUE)
print(M)
```

Output:

```r
     [,1] [,2] [,3]
[1,] "a"  "a"  "b" 
[2,] "b"  "c"  "c" 
```

### Arrays <a href="#arrays" id="arrays"></a>

Multi-dimensional data set

```r
a <- array(c('a','b'),dim = c(4,4,2))
print(a)
```

Output:

```r
, , 1
    [,1] [,2] [,3] [,4]
[1,] "a"  "a"  "a"  "a" 
[2,] "b"  "b"  "b"  "b" 
[3,] "a"  "a"  "a"  "a" 
[4,] "b"  "b"  "b"  "b" 

, , 2

     [,1] [,2] [,3] [,4]
[1,] "a"  "a"  "a"  "a" 
[2,] "b"  "b"  "b"  "b" 
[3,] "a"  "a"  "a"  "a" 
[4,] "b"  "b"  "b"  "b" 
```

### Factors <a href="#factors" id="factors"></a>

Stores the vector along with the distinct values of the elements in the vector as labels

```r
apple <- c('red','green','yellow')
factor_apple <- factor(apple)
print(factor_apple)
```

Output:

```r
red    green  yellow
Levels: green red yellow
```

### Data Frames <a href="#data_frames" id="data_frames"></a>

Tabular data objects

```r
# Create the data frame.
BMI <- 	data.frame(
   gender = c('Male', 'Female','Female'),
   height = c(182, 141.5, 165),
   weight = c(101,95, 88),
   Age = c(35,43,22)
)
print(BMI)
```

Output:

```r
  gender height weight Age
1   Male  182.0    101  35
2 Female  141.5     95  43
3 Female  165.0     88  22
```

## Loops <a href="#loops" id="loops"></a>

### Repeat Loop <a href="#repeat_loop" id="repeat_loop"></a>

```r
repeat {
   commands
   if(condition) {
      break
   }
}
```

### While Loop <a href="#while_loop" id="while_loop"></a>

```r
while (test_expression) {
   statement
}
```

### For Loop <a href="#for_loop" id="for_loop"></a>

```r
while (test_expression) {
   statement
}
```

## Working with Data <a href="#working_with_data" id="working_with_data"></a>

### Useful Packages <a href="#useful_packages" id="useful_packages"></a>

| Tasks           | Lists                                 |
| --------------- | ------------------------------------- |
| Load data       | utils, openxlsx, foreign, haven       |
| Manipulate data | tidyverse, dplyr,tidyr                |
| Visualize data  | ggplot2, lattice, plotly              |
| Modeling        | 2 + 3i                                |
| Character       | glmnet, randomForest, caret, survival |

### Import Data <a href="#import_data" id="import_data"></a>

```r
#CSV
df <- read.csv("c:/data.csv", header = T)

#Excel
df <- read.xlsx("c:/data.xlsx")
```

For the following examples, use package called datasets

### Data Exploration <a href="#data_exploration" id="data_exploration"></a>

Use dataset called mtcars from package datasets

* str(data): gives a quick overview of the rows and columns of the dataset.

```r
import Pkg; Pkg.add("RCall")
import Pkg; Pkg.add("RDatasets")
using RCall
using RDatasets
mtcars = dataset("datasets","mtcars");
@rput mtcars

R"str(mtcars)"
```

Output:

```r
e[?25le[2Ke[?25hUnable to automatically install 'Zlib' from '/home/runner/.julia/packages/Zlib_jll/BGVLi/Artifacts.toml'
```

* head(data,n) and tail(data,n)

head(): Top n elements

tail(): Bottom n elements

```r
@rput mtcars

R"print(head(mtcars, n = 3))"
R"print(tail(mtcars, n = 3))"
```

Output:

```r
LoadError: UndefVarError: @rput not defined
in expression starting at none:1
```

### Descriptive Statistics <a href="#descriptive_statistics" id="descriptive_statistics"></a>

* summary(data): gives descriptive statistics for each variable

Common Functions

| Tasks               | Functions  |
| ------------------- | ---------- |
| Mean                | mean()     |
| Standard deviation  | sd()       |
| Variance            | var()      |
| Minimum             | min()      |
| Maximum             | max()      |
| Median              | median()   |
| Range of values     | range()    |
| Sample quantiles    | quantile() |
| Interquartile range | IQR()      |

#### **Case of missing values**

* na.rm = T

```r
@rput mtcars

R"print(mean(mtcars$MPG, na.rm = T))"
```

Output:

```r
LoadError: UndefVarError: @rput not defined
in expression starting at none:1
```

### Basic Plots <a href="#basic_plots" id="basic_plots"></a>

* plot()

```r
using RCall
using RDatasets
mtcars = dataset("datasets","mtcars");

@rput mtcars

R"
png('plot2.png')
plot(mtcars$Disp, mtcars$DRat)
dev.off()
"
```

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

* barplot()

```r
R"
png('barplot2.png')
barplot(mtcars$Cyl,main = 'Number of Cylinders',xlab = 'cyl', col='blue',horiz = FALSE)
dev.off()
"
```

Output:

<figure><img src="/files/7ZMOFifTcN4Jxy1WgAMB" alt=""><figcaption></figcaption></figure>

* histogram()

```r
R"
png('histogram.png')
hist(mtcars$Disp,main = 'Displacement (cu.in.)',xlab = 'disp', col='red')
dev.off()
"
```

Output:

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

* boxplot()

```r
using RCall
using RDatasets
mtcars = dataset("datasets","mtcars");

@rput mtcars

R"
png('boxplot2.png')
boxplot(mtcars$Disp)
dev.off()
"
```

Output:

<figure><img src="/files/9qoY24P888FZF9XJ2Xjo" alt=""><figcaption></figcaption></figure>

* qqplot() or qqnorm(): check whether the data is normally distributed
* qqline(): adds a reference line

```r
using RCall
using RDatasets
mtcars = dataset("datasets","mtcars");

@rput mtcars

R"
png('qqnorm.png')
qqnorm(mtcars$Disp)
qqline(mtcars$Disp)
dev.off()
"
```

Output:

```r
ArgumentError: Package RCall not found in current path:
- Run `import Pkg; Pkg.add("RCall")` to install the RCall package.

```

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

## Statistical Analysis <a href="#statistical_analysis" id="statistical_analysis"></a>

| Analysis             | Continuous Outcome(Y) | Binary Outcome(Y) |
| -------------------- | --------------------- | ----------------- |
| Correlation Analysis |                       |                   |
| X: Continuous        | cor.test()            | t.test()          |
| X: Categorical       | t.test(), ANOVA()     | chisq.test()      |
| Regression Model     | lm()                  | glm()             |


---

# 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.bcbi.brown.edu/codiac-for-health/computing/r-basics.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.
