In our first post on R programming language I covered how to download R and R Studio, understanding the structure of R Studio, and loading datasets into R. I meant to continue the series (as I always do), but couldn't get back to it any sooner. Recently I've started working on a workshop where I'll be teaching R to the beginners. So, I thought this would be the best time to add more content to this series as well.
In this Part 2 I'll write about the following - Understanding prompts of R Studio, doing basic calculations in R, all about variables, functions, the concept of vector in R, and data frame. Some of these I should have covered in Part 1, but better late than never!
Prompts in R Studio
- In console a new line starts with >, means it is waiting for us to communicate
- If we give it an incomplete command then it returns +. Press esc button to return to a new line.
- To quit R type q()
Doing Basic Calculations in R
- The order of arithmetic operations is (left [done first] to right [done last]) : ^ / * - +
- ^ is used for raised to the power of, followed by division, multiplication, subtraction and addition.
- At the prompt, we enter the expression that we want evaluated and when we hit enter, it will compute the result for us. For example: > 10 + 22 will return [1] 32
All About Variables
- Variables are the symbols that store assigned values. We can store a computation under a new variable or change the existing value of an old variable.
- Variable names in R are case sensitive (upper or lower case).
- It is a good practice to assign meaningful variable names that helps to refer to easily for complex calculations.
To assign a value: variable_name <- value
Example: x <- 100
ALERT! Reserved Symbols!
In all programming languages certain symbols are reserved for specific purposes. The reserved symbols in R are - c q t C D F I T (So, don't use them for your personal variables ^-^)
Functions
A function is a sub-program that performs a specific task. For example, to find a square root of a given value. It helps to avoid repetition and easy execution in future.
Try this code to understand how functions work -
firstFunction <- function(n){n*n}
This function named firstFunction is supposed to return square of any integer. Test it out by assigning different values to the function. Think of what other functions you can possibly write.
Vector
Vector has different meanings in different contexts. In math and physics, a vector is an element with both value and direction. But in R, vector is a sequence of data elements of the same basic type. It can be defined by concatenating the members in a set c(). Example: x <- c(1, 2, 4, 5).
Once we have a vector of numbers we can apply certain built-in functions to them to get useful summaries. For example:
> sum(x) ## sums the values in the vector
> length(x) ## produces the number of values in the vector, ie its length
> mean(x) ## the average (mean)
Data Frame
A data frame can be created by defining different variables for each column as vectors and then joining them together.
Example: Let us assume we have a list of different fruits with their names, colors and size.
> name <- c("apple", "banana", "peach", "watermelon", "grape")
> color <- c("red", "yellow", "peach", "green", "red")
> size_cm <- c(10, 15, 8, 40, 2)
Then we add these three columns together to create the data frame names fruits.data.
> fruits.data <- data.frame(name, color, size_cm)
To see the values of the data frame -
> fruits.data
name color size_cm
1 apple red 10
2 banana yellow 15
3 peach peach 8
4 watermelon green 40
5 grape red 2
--------------------------------------------------------------------------------------------------------------------------
I think we've covered a lot of basics concepts already, so I'll stop here today. In the next post of this series, I'll write about setting work directory, manipulating datasets, and playing around with some plots/visualizations, and hope I can make it sometime soon!
No comments:
Post a Comment