R Flashcards

1
Q

How to check the type?

A

is. numeric(MyFirstVector)
is. integer(MyFirstVector)
is. double(MyFirstVector)
is. character(v3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to create a vector?

A

x – c(1, 5, 4, 9, 0)
x – c(1, 5.4, TRUE, “hello”)
x – 1:7
seq(1, 3, by=0.2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Brackets

w[-1]

A

x[c(2, 4)] # access 2nd and 4th element

w[-1] # access all but 1st element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

for loop

A

for (x in 1:10) {
print(x)
}

fruits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

type

A

typeof()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to get some info. about functions?

A

?rnorm()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Install and activate a package

A

install.packages(“ggplot2”)

library(ggplot2) #activate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Matrix Indexation

A

[2,3]
[2,]
[.3]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to remove a vector?

How to remove a dataframe?

A

rm(vector1, vector2,vector3)

rm(mydf)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Building a matrix, 3 ways

A

rbind()

cbind()

matrix(my.data, 4, 5) , byrow =T

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to name a vector? +remove name

A

vector – c(“John Doe”, “poker player”)
names(vector) – c(“Name”, “Profession”)

unname(vector)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to name rows and cols of a matrix?

A

rownames(baskets.team) – c(“Granny”, “Geraldine”)

colnames(baskets.team) – c(“1st”, “2nd”, “3th”, “4th”, “5th”, “6th”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

matplot

legend

A

matplot(t(FieldGoals), type=”b”, pch=15:18, col=c(1:4,6))

legend(“bottomleft”, inset=0.01, legend = Players, pch=15:18, col=c(1:4,6), horiz=F)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Subsetting

A

Games[1:3,6:10]
Games[c(1,10),]
Games[,c(“2008”,”2009”)]

Games[1,]
Games[1,,drop=F]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Create a function and set default values

A

myplot

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Main diff between matrix and dataframe?

A

All elements of the matrix have same type

17
Q

Select it manually and read a csv file

A

stats

18
Q

Set working directory

A

getwd()

setwd(“C:/Users/266507/OneDrive - American Airlines, Inc/Documents/R”)

19
Q

Read a csv file

A

stats

20
Q

find no. of rows and columns:

A

nrow(stats)

ncol(stats)

21
Q

head and tail:

A

head(stats, n=10)

tail(stats)

22
Q

summary of the data:

A

str(stats)

summary(stats)

23
Q

Using the $ sign:

A

stats$Country.Name

stats[,”Country.Name”]

stats$Country.Name[2]

24
Q

Categories:

A

levels(stats$Country.Name)

25
Q

A diff between Matrix and DataFrame:

A

When we get one row of a matrix, it become a vector but it’s not the case in DataFrame
But it’s gonna be a vector when we get a col in a dataframe

26
Q

How to check if it’s a dataframe

A

is.data.frame(stats[1,])

27
Q

How to preserve a col of a dataframe as a dataframe when subsetting:

A

stats[,1,drop=F]

28
Q

Add and remove a col to a dataframe

A

stats$MyCalc

29
Q

Filtering dataframes

A

stats[stats$Birth.rate>40 & stats$Internet.users<2, ]

stats[stats$Income.Group == “High income”, ]

30
Q

qplot

A

qplot(data=stats, x=Internet.users)

qplot(data=stats, x=Income.Group, y=Birth.rate)

qplot(data=stats, x=Income.Group, y=Birth.rate, size=I(3), color=I(“blue”))

qplot(data=stats, x=Internet.users, y=Birth.rate, size=I(5), color=Income.Group)

qplot(data=stats, x=Income.Group, y=Birth.rate, geom=”boxplot”)

31
Q

Creating DataFrames

A

mydf

32
Q

Merging Data Frames

A

merged

33
Q

Change the shape on qplot

A

qplot(data=merged, x=Internet.users, y=Birth.rate, color=Region, size=I(5), shape=I(19))

34
Q

Control the transparency on qplot

A

qplot(data=merged, x=Internet.users, y=Birth.rate, color=Region, size=I(5), shape=I(19), alpha=I(0.6))

35
Q

Set the title in qplot

A

qplot(data=merged, x=Internet.users, y=Birth.rate, color=Region, size=I(5), shape=I(19), alpha=I(0.6), main=”Birth Rate vs Internet Users”)