R PROGRAMMING Flashcards

1
Q

Sorting

A

The function sort, sorts
a vector in increasing order.

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

Order

A

It takes vectors as input, and returns the vector of indexes that sorts the input vector.

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

example for order

A

ind <- order(murders$total)
# This gives the index of total murder in increasing order
murders$abb[ind]

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

Example For order

A

x <- c(5,4,6,3,1,0)
index <- order(x)
x[index]

output
ans: 0 1 3 4 5 6

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

MAX

A

If we are only interested in the value, we can use max function

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

Which.Max

A

If we want the index, we use which.max function

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

RANK

A

For any given vector it returns a vector with the rank of the first
entry, second entry, etc., of the input vecto

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

Which.max example

A

i_max <- which.max(murders$total)
murders$state[i_max]
#> [1] “California”

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

HOW TO REORDER USING ORDER FUNCTION

A

STEP 1 : FIND THE INDEX AT WHICH THE VECTORS ARE SORTED.
STEP 2: REORDER THE OBJECT USING ASSIGNED OBJECT INSIDE THE SQUARE BRACKET []

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

TAKE input from the user

A

When we are working with R in an interactive session, we can use readline() function to take input from the user (terminal)

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

TAKE input from the user EXAMPLE

A

my.name <- readline(prompt=”Enter name: “)
my.age <- readline(prompt=”Enter age: “)
# convert character into integer
my.age <- as.integer(my.age)
print(paste(“Hi,”, my.name, “next year you will be”, my.age+1, “years old.”))

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