RStudio notes 1

Scott
3 min readMay 5, 2020

--

Download R.

Download R Studio interpreter.

Open RStudio.

Add a file

Top Left

Import a CSV

This will ask if you want to install a component to get CSVs. Say yes. It will install automatically.

Click Browse,

Choose your file and then click Import at the bottom right.

You should see your CSV displayed.

Check which Libraries are available

bottom right quadrant

Add a Library via command

Go to the RStudio console and type.

Here we import “assertthat”
library("assertthat")

Add a Library via Script

library('libraryName')

Install new Library

Click Install

Install Library via command

Search through docs

Two ways to assign variables

Comments

# hash tag lets you write a comment. 

Multiline comment

'''
Hello
'''

Modulus operator

10 %% 2
# outputs: 0
10 %% 3
# outputs: 1

Get the type of a variable

s = "a"class(a)
# "character"

R does not use strings, it only uses character for multi and single character objects.

Convert type

i = 10
class(i)
# output: "integer"
i = as.character(i)
class(i)
# output: "character"

check type

is.character(d)

Basic Operators

10 + 20
10 - 20
10 * 20
10/20 # result is double
10 %% 20 # modulus
10 %/% 20 # result is integer

List (array)

Unlike Python and most languages indices start from 1 in r.

l = list(1, 2, 3, 4, 5, "cat")

Vector

Unlike Python and most languages indices start from 1 in r.

vec = c(1, 2, 3, 4, "cat")

It concatenates for each character in the vector.

Concatenation doesn’t always do that.

Create a Matrix

matrix(c(1, 3, 4, 5), nrow=2, byrow = TRUE)

Create DataFrame

students = data.frame(
Name = c("X", "Y"),
Age = c(10, 9),
class = c('5th', '4th')
)

If else

if (condition) {} else if (other condition) {} else {}

How to get a user input

usernum = as.integer(readline(prompt = "Enter a number"))

You have to run lines separately when taking a user input in RStudio.

--

--

No responses yet