Last updated on 2024-07-02 | Edit this page
Overview
Questions
- What will these lessons not cover?
- What are the basic features of the R language?
- What are the most common objects in R?
Objectives
- Be able to create the most common R objects including vectors
- Understand that vectors have modes, which correspond to the type ofdata they contain
- Be able to use arithmetic operators on R objects
- Be able to retrieve (subset), name, or replace, values from avector
- Be able to use logical operators in a subsetting operation
“The fantastic world of R awaits you” OR “Nobody wants to learn howto use R”
Before we begin this lesson, we want you to be clear on the goal ofthe workshop and these lessons. We believe that every learner canachieve competency with R. You have reached competencywhen you find that you are able to use R to handle commonanalysis challenges in a reasonable amount of time (whichincludes time needed to look at learning materials, search for answersonline, and ask colleagues for help). As you spend more time using R(there is no substitute for regular use and practice) you will findyourself gaining competency and even expertise. The more familiar youget, the more complex the analyses you will be able to carry out, withless frustration, and in less time - the fantastic world of R awaitsyou!
What these lessons will not teach you
Nobody wants to learn how to use R. People want to learn how to use Rto analyze their own research questions! Ok, maybe some folks learn Rfor R’s sake, but these lessons assume that you want to start analyzinggenomic data as soon as possible. Given this, there are many valuablepieces of information about R that we simply won’t have time to cover.Hopefully, we will clear the hurdle of giving you just enough knowledgeto be dangerous, which can be a high bar in R! We suggest you look intothe additional learning materials in the tip box below.
Here are some R skills we will not cover in theselessons
- How to create and work with R matrices
- How to create and work with loops and conditional statements, andthe “apply” family of functions (which are super useful, read thisblog post to learn more about these functions)
- How to do basic string manipulations (e.g.finding patterns in textusing grep, replacing text)
- How to plot using the default R graphic tools (we willcover plot creation, but will do so using the popular plotting package
ggplot2
) - How to use advanced R statistical functions
Tip: Where to learn more
The following are good resources for learning more about R. Some ofthem can be quite technical, but if you are a regular R user you mayultimately need this technical knowledge.
- Rfor Beginners: By Emmanuel Paradis and a great starting point
- The R Manuals:Maintained by the R project
- R contributeddocumentation: Also linked to the R project; importantly there arematerials available in several languages
- R for Data Science: Awonderful collection by noted R educators and developers GarrettGrolemund and Hadley Wickham
- PracticalData Science for Stats: Not exclusively about R usage, but a nicecollection of pre-prints on data science and applications for R
- Programming in RSoftware Carpentry lesson: There are several Software Carpentrylessons in R to choose from
Creating objects in R
Reminder
At this point you should be coding along in the“genomics_r_basics.R” script we created in the lastepisode. Writing your commands in the script (and commenting it) willmake it easier to record what you did and why.
What might be called a variable in many languages is called anobject in R.
To create an object you need:
- a name (e.g.‘first_value’)
- a value (e.g.‘1’)
- the assignment operator (‘<-’)
In your script, “genomics_r_basics.R”, using the Rassignment operator ‘<-’, assign ‘1’ to the object ‘first_value’ asshown. Remember to leave a comment in the line above (using the ‘#’) toexplain what you are doing:
R
# this line creates the object 'first_value' and assigns it the value '1'first_value <- 1
Next, run this line of code in your script. You can run a line ofcode by hitting the Run button that is just above the firstline of your script in the header of the Source pane or you can use theappropriate shortcut:
- Windows execution shortcut: Ctrl+Enter
- Mac execution shortcut: Cmd(⌘)+Enter
To run multiple lines of code, you can highlight all the line youwish to run and then hit Run or use the shortcut key combolisted above.
In the RStudio ‘Console’ you should see:
OUTPUT
first_value <- 1>
The ‘Console’ will display lines of code run from a script and anyoutputs or status/warning/error messages (usually in red).
In the ‘Environment’ window you will also get a table:
Values | |
---|---|
first_value | 1 |
The ‘Environment’ window allows you to keep track of the objects youhave created in R.
Exercise: Create some objects in R
Create the following objects; give each object an appropriate name(your best guess at what name to use is fine):
- Create an object that has the value of number of pairs of humanchromosomes
- Create an object that has a value of your favorite gene name
- Create an object that has this URL as its value: “ftp://ftp.ensemblgenomes.org/pub/bacteria/release-39/fasta/bacteria_5_collection/escherichia_coli_b_str_rel606/”
- Create an object that has the value of the number of chromosomes ina diploid human cell
Here as some possible answers to the challenge:
R
human_chr_number <- 23gene_name <- 'pten'ensemble_url <- 'ftp://ftp.ensemblgenomes.org/pub/bacteria/release-39/fasta/bacteria_5_collection/escherichia_coli_b_str_rel606/'human_diploid_chr_num <- 2 * human_chr_number
Naming objects in R
Here are some important details about naming objects in R.
- Avoid spaces and special characters: Object namescannot contain spaces or the minus sign (
-
). You can use‘_’ to make names more readable. You should avoid using specialcharacters in your object name (e.g.! @ # . , etc.). Also, object namescannot begin with a number. - Use short, easy-to-understand names: You shouldavoid naming your objects using single letters (e.g.‘n’, ‘p’, etc.).This is mostly to encourage you to use names that would make sense toanyone reading your code (a colleague, or even yourself a year fromnow). Also, avoiding excessively long names will make your code morereadable.
- Avoid commonly used names: There are several namesthat may already have a definition in the R language (e.g.‘mean’,‘min’, ‘max’). One clue that a name already has meaning is that if youstart typing a name in RStudio and it gets a colored highlight orRStudio gives you a suggested autocompletion you have chosen a name thathas a reserved meaning.
- Use the recommended assignment operator: In R, weuse ‘<-’ as the preferred assignment operator. ‘=’ works too, but ismost commonly used in passing arguments to functions (more on functionslater). There is a shortcut for the R assignment operator:
- Windows execution shortcut: Alt+-
- Mac execution shortcut: Option+-
There are a few more suggestions about naming and style you may wantto learn more about as you write more R code. There are several “styleguides” that have advice. One of the more widely used is the tidyverse R styleguide, but there is also a Google R styleguide, and Jean Fan’s Rstyle guide, among others.
Tip: Pay attention to warnings in the script console
If you enter a line of code in your script that contains an error,RStudio may give you an error message and underline this mistake.Sometimes these messages are easy to understand, but often the messagesmay need some figuring out. Paying attention to these warnings will helpyou avoid mistakes. In the example below, our object name has a space,which is not allowed in R. The error message does not say this directly,but R is “not sure” about how to assign the name to “human_ chr_number”when the object name we want is “human_chr_number”.
![Intro to R and RStudio for Genomics: R Basics (1) Intro to R and RStudio for Genomics: R Basics (1)](https://i0.wp.com/gwu-libraries.github.io/fig/rstudio_script_warning.png)
Reassigning object names or deleting objects
Once an object has a value, you can change that value by overwritingit. R will not give you a warning or error if you overwriting an object,which may or may not be a good thing depending on how you look atit.
R
# gene_name has the value 'pten' or whatever value you used in the challenge.# We will now assign the new value 'tp53'gene_name <- 'tp53'
You can also remove an object from R’s memory entirely. Therm()
function will delete the object.
R
# delete the object 'gene_name'rm(gene_name)
If you run a line of code that has only an object name, R willnormally display the contents of that object. In this case, we are toldthe object no longer exists.
ERROR
Error: object 'gene_name' not found
Understanding object data types (classes and modes)
In R, every object has several properties:
- Length: How many distinct values are held in thatobject
- Mode: What is the classification (type) of thatobject.
- Class: A property assigned to an object thatdetermines how a function will operate on it.
We will get to the “length” property later in the lesson. The“mode” property corresponds to the type of dataan object represents and the “class” propertydetermines how functions will work with that object.
Tip: Classess vs.modes
The difference between modes and classes is a bitconfusing and the subject of several onlinediscussions. Often, these terms are used interchangeably. Do youreally need to know the difference?
Well, perhaps. This section is important for you to have a betterunderstanding of how R works and how to write usable code. However, youmight not come across a situation where the difference is crucial whileyou are taking your first steps in learning R. However, the overarchingconcept—that objects in R have these properties and that you canuse functions to check or change them—is very important!
In this lesson we will mostly stick to mode but wewill throw in a few examples of the class()
andtypeof()
so you can see some examples of where it may makea difference.
The most common modes you will encounter in R are:
Mode (abbreviation) | Type of data |
---|---|
Numeric (num) | Numbers such floating point/decimals (1.0, 0.5, 3.14), there arealso more specific numeric types (dbl - Double, int - Integer). Thesedifferences are not relevant for most beginners and pertain to how thesevalues are stored in memory |
Character (chr) | A sequence of letters/numbers in single ’’ or double ” ” quotes |
Logical | Boolean values - TRUE or FALSE |
There are a few other modes (i.e.“complex”, “raw” etc.) but theseare the three we will work with in this lesson.
Data types are familiar in many programming languages, but also innatural language where we refer to them as the parts of speech,e.g.nouns, verbs, adverbs, etc. Once you know if a word - perhaps anunfamiliar one - is a noun, you can probably guess you can count it andmake it plural if there is more than one (e.g., 1 Tuatara, or 2Tuataras). If something is a adjective, you can usually change it intoan adverb by adding “-ly” (e.g., jejune vs.jejunely). Depending on the context, you may need to decide if a word isin one category or another (e.g “cut” may be a noun when it’s on yourfinger, or a verb when you are preparing vegetables). These conceptshave important analogies when working with R objects.
Exercise: Create objects and check their modes
Create the following objects in R, then use the mode()
function to verify their modes. Try to guess what the mode will bebefore you look at the solution
chromosome_name <- 'chr02'
od_600_value <- 0.47
chr_position <- '1001701'
spock <- TRUE
pilot <- Earhart
R
mode(chromosome_name)
OUTPUT
[1] "character"
R
mode(od_600_value)
OUTPUT
[1] "numeric"
R
mode(chr_position)
OUTPUT
[1] "character"
R
mode(spock)
OUTPUT
[1] "logical"
R
mode(pilot)
ERROR
Error in eval(expr, envir, enclos): object 'pilot' not found
Exercise: Create objects and check their class using “class”
Using the objects created in the previous challenge, use theclass()
function to check their classes.
R
class(chromosome_name)
OUTPUT
[1] "character"
R
class(od_600_value)
OUTPUT
[1] "numeric"
R
class(chr_position)
OUTPUT
[1] "character"
R
class(spock)
OUTPUT
[1] "logical"
R
class(pilot)
ERROR
Error in eval(expr, envir, enclos): object 'pilot' not found
Notice that in the two challenges, mode()
andclass()
return the same results. This time…
Notice from the solution that even if a series of numbers is given asa value R will consider them to be in the “character” mode if they areenclosed as single or double quotes. Also, notice that you cannot take astring of alphanumeric characters (e.g.Earhart) and assign as a valuefor an object. In this case, R looks for an object namedEarhart
but since there is no object, no assignment can bemade. If Earhart
did exist, then the mode ofpilot
would be whatever the mode of Earhart
was originally. If we want to create an object called pilot
that was the name “Earhart”, we need to encloseEarhart
in quotation marks.
R
pilot <- "Earhart"mode(pilot)
OUTPUT
[1] "character"
R
pilot <- "Earhart"typeof(pilot)
OUTPUT
[1] "character"
Mathematical and functional operations on objects
Once an object exists (which by definition also means it has a mode),R can appropriately manipulate that object. For example, objects of thenumeric modes can be added, multiplied, divided, etc. R provides severalmathematical (arithmetic) operators including:
Operator | Description |
---|---|
+ | addition |
- | subtraction |
* | multiplication |
/ | division |
^ or ** | exponentiation |
a%/%b | integer division (division where the remainder is discarded) |
a%%b | modulus (returns the remainder after division) |
These can be used with literal numbers:
R
(1 + (5 ** 0.5))/2
OUTPUT
[1] 1.618034
and importantly, can be used on any object that evaluates to(i.e.interpreted by R) a numeric object:
R
# multiply the object 'human_chr_number' by 2human_chr_number * 2
OUTPUT
[1] 46
Exercise: Compute the golden ratio
One approximation of the golden ratio (φ) can be found by taking thesum of 1 and the square root of 5, and dividing by 2 as in the exampleabove. Compute the golden ratio to 3 digits of precision using thesqrt()
and round()
functions. Hint: rememberthe round()
function can take 2 arguments.
R
round((1 + sqrt(5))/2, digits = 3)
OUTPUT
[1] 1.618
Notice that you can place one function inside of another.
Vectors
Vectors are probably the most used commonly used object type in R.A vector is a collection of values that are all of the same type(numbers, characters, etc.). One of the most common ways tocreate a vector is to use the c()
function - the“concatenate” or “combine” function. Inside the function you may enterone or more values; for multiple values, separate each value with acomma:
R
# Create the SNP gene name vectorsnp_genes <- c("OXTR", "ACTN3", "AR", "OPRM1")
Vectors always have a mode and alength. You can check these with themode()
and length()
functions respectively.Another useful function that gives both of these pieces of informationis the str()
(structure) function.
R
# Check the mode, length, and structure of 'snp_genes'mode(snp_genes)
OUTPUT
[1] "character"
R
length(snp_genes)
OUTPUT
[1] 4
R
str(snp_genes)
OUTPUT
chr [1:4] "OXTR" "ACTN3" "AR" "OPRM1"
Vectors are quite important in R. Another data type that we will workwith later in this lesson, data frames, are collections of vectors. Whatwe learn here about vectors will pay off even more when we start workingwith data frames.
Creating and subsetting vectors
Let’s create a few more vectors to play around with:
R
# Some interesting human SNPs# while accuracy is important, typos in the data won't hurt you heresnps <- c("rs53576", "rs1815739", "rs6152", "rs1799971")snp_chromosomes <- c("3", "11", "X", "6")snp_positions <- c(8762685, 66560624, 67545785, 154039662)
Once we have vectors, one thing we may want to do is specificallyretrieve one or more values from our vector. To do so, we usebracket notation. We type the name of the vectorfollowed by square brackets. In those square brackets we place the index(e.g.a number) in that bracket as follows:
OUTPUT
[1] "rs6152"
In R, every item your vector is indexed, starting from the first item(1) through to the final number of items in your vector. You can alsoretrieve a range of numbers:
R
# get the 1st through 3rd value in the snp vectorsnps[1:3]
OUTPUT
[1] "rs53576" "rs1815739" "rs6152"
If you want to retrieve several (but not necessarily sequential)items from a vector, you pass a vector of indices; avector that has the numbered positions you wish to retrieve.
R
# get the 1st, 3rd, and 4th value in the snp vectorsnps[c(1, 3, 4)]
OUTPUT
[1] "rs53576" "rs6152" "rs1799971"
There are additional (and perhaps less commonly used) ways ofsubsetting a vector (see theseexamples). Also, several of these subsetting expressions can becombined:
R
# get the 1st through the 3rd value, and 4th value in the snp vector# yes, this is a little silly in a vector of only 4 values.snps[c(1:3,4)]
OUTPUT
[1] "rs53576" "rs1815739" "rs6152" "rs1799971"
Adding to, removing, or replacing values in existing vectors
Once you have an existing vector, you may want to add a new item toit. To do so, you can use the c()
function again to addyour new value:
R
# add the gene "CYP1A1" and "APOA5" to our list of snp genes# this overwrites our existing vectorsnp_genes <- c(snp_genes, "CYP1A1", "APOA5")
We can verify that “snp_genes” contains the new gene entry
R
snp_genes
OUTPUT
[1] "OXTR" "ACTN3" "AR" "OPRM1" "CYP1A1" "APOA5"
Using a negative index will return a version of a vector with thatindex’s value removed:
R
snp_genes[-6]
OUTPUT
[1] "OXTR" "ACTN3" "AR" "OPRM1" "CYP1A1"
We can remove that value from our vector by overwriting it with thisexpression:
R
snp_genes <- snp_genes[-6]snp_genes
OUTPUT
[1] "OXTR" "ACTN3" "AR" "OPRM1" "CYP1A1"
We can also explicitly rename or add a value to our index usingdouble bracket notation:
R
snp_genes[6]<- "APOA5"snp_genes
OUTPUT
[1] "OXTR" "ACTN3" "AR" "OPRM1" "CYP1A1" "APOA5"
Exercise: Examining and subsetting vectors
Answer the following questions to test your knowledge of vectors
Which of the following are true of vectors in R? A) All vectors havea mode or a length
B) All vectors have a mode and a length
C) Vectors may have different lengths
D) Items within a vector may be of different modes
E) You can use the c()
to add one or more items to anexisting vector
F) You can use the c()
to add a vector to an existingvector
- False - Vectors have both of these properties
- True
- True
- False - Vectors have only one mode (e.g.numeric, character); allitems in
a vector must be of this mode. - True
- True
Logical Subsetting
There is one last set of cool subsetting capabilities we want tointroduce. It is possible within R to retrieve items in a vector basedon a logical evaluation or numerical comparison. For example, let’s saywe wanted get all of the SNPs in our vector of SNP positions that weregreater than 100,000,000. We could index using the ‘>’ (greater than)logical operator:
R
snp_positions[snp_positions > 100000000]
OUTPUT
[1] 154039662
In the square brackets you place the name of the vector followed bythe comparison operator and (in this case) a numeric value. Some of themost common logical operators you will use in R are:
Operator | Description |
---|---|
< | less than |
<= | less than or equal to |
> | greater than |
>= | greater than or equal to |
== | exactly equal to |
!= | not equal to |
!x | not x |
a | b | a or b |
a & b | a and b |
The magic of programming
The reason why the expressionsnp_positions[snp_positions > 100000000]
works can bebetter understood if you examine what the expression “snp_positions >100000000” evaluates to:
R
snp_positions > 100000000
OUTPUT
[1] FALSE FALSE FALSE TRUE
The output above is a logical vector, the 4th element of which isTRUE. When you pass a logical vector as an index, R will return the truevalues:
R
snp_positions[c(FALSE, FALSE, FALSE, TRUE)]
OUTPUT
[1] 154039662
If you have never coded before, this type of situation starts toexpose the “magic” of programming. We mentioned before that in thebracket notation you take your named vector followed by brackets whichcontain an index: named_vector[index]. The “magic” isthat the index needs to evaluate to a number. So, even if itdoes not appear to be an integer (e.g.1, 2, 3), as long as R canevaluate it, we will get a result. That our expressionsnp_positions[snp_positions > 100000000]
evaluates to anumber can be seen in the following situation. If you wanted to knowwhich index (1, 2, 3, or 4) in our vector of SNPpositions was the one that was greater than 100,000,000?
We can use the which()
function to return the indices ofany item that evaluates as TRUE in our comparison:
R
which(snp_positions > 100000000)
OUTPUT
[1] 4
Why this is important
Often in programming we will not know what inputs and values will beused when our code is executed. Rather than put in a pre-determinedvalue (e.g 100000000) we can use an object that can take on whatevervalue we need. So for example:
R
snp_marker_cutoff <- 100000000snp_positions[snp_positions > snp_marker_cutoff]
OUTPUT
[1] 154039662
Ultimately, it’s putting together flexible, reusable code like thisthat gets at the “magic” of programming!
A few final vector tricks
Finally, there are a few other common retrieve or replace operationsyou may want to know about. First, you can check to see if any of thevalues of your vector are missing (i.e.are NA
, that standsfor not avaliable
). Missing data will get a more detailedtreatment later, but the is.NA()
function will return alogical vector, with TRUE for any NA value:
R
# current value of 'snp_genes':# chr [1:7] "OXTR" "ACTN3" "AR" "OPRM1" "CYP1A1" NA "APOA5"is.na(snp_genes)
OUTPUT
[1] FALSE FALSE FALSE FALSE FALSE FALSE
Sometimes, you may wish to find out if a specific value (or severalvalues) is present a vector. You can do this using the comparisonoperator %in%
, which will return TRUE for any value in yourcollection that is in the vector you are searching:
R
# current value of 'snp_genes':# chr [1:7] "OXTR" "ACTN3" "AR" "OPRM1" "CYP1A1" NA "APOA5"# test to see if "ACTN3" or "APO5A" is in the snp_genes vector# if you are looking for more than one value, you must pass this as a vectorc("ACTN3","APOA5") %in% snp_genes
OUTPUT
[1] TRUE TRUE
Tip: What’s the difference between the%in% and the
==` operators?
The %in%
operator is used to test if the elements of avector are present in another vector. In the example above, if both“ACTN3” and “APOA5” are in the vector snp_genes
, then Rwill return TRUE TRUE
since they are both present. If“ACTN3” is but “APOA5” is not in snp_genes
, then R willreturn TRUE FALSE
. The ==
operator is used totest if two vectors are exactly equal. For example, if you wanted toknow if the vector c(1, 2, 3)
was equal to the vectorc(1, 2, 3)
, you could use the ==
operator. Onetrick people sometimes use is to check a single value against a vectorwith the ==
operator. For example, if you wanted to know ifthe value 1
was in the vector c(1, 2, 3)
, youcould use the expression 1 == c(1, 2, 3)
. This would returnTRUE FALSE FALSE
since the value 1
is only inthe first position of the vector c(1, 2, 3)
. Note thatc(1, 2) == c(1, 2, 3)
will return an error since thevectors are of different lengths.
Review Exercise 1
What data modes are the following vectors? a. snps
b. snp_chromosomes
c.snp_positions
R
mode(snps)
OUTPUT
[1] "character"
R
mode(snp_chromosomes)
OUTPUT
[1] "character"
R
mode(snp_positions)
OUTPUT
[1] "numeric"
Review Exercise 2
Add the following values to the specified vectors: a. To thesnps
vector add: “rs662799”
b. To the snp_chromosomes
vector add: 11
c.To the snp_positions
vector add: 116792991
R
snps <- c(snps, "rs662799")snps
OUTPUT
[1] "rs53576" "rs1815739" "rs6152" "rs1799971" "rs662799"
R
snp_chromosomes <- c(snp_chromosomes, "11") # did you use quotes?snp_chromosomes
OUTPUT
[1] "3" "11" "X" "6" "11"
R
snp_positions <- c(snp_positions, 116792991)snp_positions
OUTPUT
[1] 8762685 66560624 67545785 154039662 116792991
Review Exercise 3
Make the following change to the snp_genes
vector:
Hint: Your vector should look like this in ‘Environment’:chr [1:7] "OXTR" "ACTN3" "AR" "OPRM1" "CYP1A1" NA "APOA5"
.If not recreate the vector by running this expression:snp_genes <- c("OXTR", "ACTN3", "AR", "OPRM1", "CYP1A1", NA, "APOA5")
- Create a new version of
snp_genes
that does not containCYP1A1 and then - Add 2 NA values to the end of
snp_genes
R
snp_genes <- snp_genes[-5]snp_genes <- c(snp_genes, NA, NA)snp_genes
OUTPUT
[1] "OXTR" "ACTN3" "AR" "OPRM1" "APOA5" NA NA
Review Exercise 4
Using indexing, create a new vector named combined
thatcontains:
- The the 1st value in
snp_genes
- The 1st value in
snps
- The 1st value in
snp_chromosomes
- The 1st value in
snp_positions
R
combined <- c(snp_genes[1], snps[1], snp_chromosomes[1], snp_positions[1])combined
OUTPUT
[1] "OXTR" "rs53576" "3" "8762685"
Review Exercise 5
What type of data is combined
?
R
typeof(combined)
OUTPUT
[1] "character"
Lists
Lists are quite useful in R, but we won’t be using them in thegenomics lessons. That said, you may come across lists in the way thatsome bioinformatics programs may store and/or return data to you. One ofthe key attributes of a list is that, unlike a vector, a list maycontain data of more than one mode. Learn more about creating and usinglists using this nice tutorial. Inthis one example, we will create a named list and show you how toretrieve items from the list.
R
# Create a named list using the 'list' function and our SNP examples# Note, for easy reading we have placed each item in the list on a separate line# Nothing special about this, you can do this for any multiline commands# To run this command, make sure the entire command (all 4 lines) are highlighted# before running# Note also, as we are doing all this inside the list() function use of the# '=' sign is good stylesnp_data <- list(genes = snp_genes, refference_snp = snps, chromosome = snp_chromosomes, position = snp_positions)# Examine the structure of the liststr(snp_data)
OUTPUT
List of 4 $ genes : chr [1:7] "OXTR" "ACTN3" "AR" "OPRM1" ... $ refference_snp: chr [1:5] "rs53576" "rs1815739" "rs6152" "rs1799971" ... $ chromosome : chr [1:5] "3" "11" "X" "6" ... $ position : num [1:5] 8.76e+06 6.66e+07 6.75e+07 1.54e+08 1.17e+08
To get all the values for the position
object in thelist, we use the $
notation:
R
# return all the values of position objectsnp_data$position
OUTPUT
[1] 8762685 66560624 67545785 154039662 116792991
To get the first value in the position
object, use the[]
notation to index:
R
# return first value of the position objectsnp_data$position[1]
OUTPUT
[1] 8762685
Key Points
- Effectively using R is a journey of months or years. Still you don’thave to be an expert to use R and you can start using and analyzing yourdata with with about a day’s worth of training
- It is important to understand how data are organized by R in a givenobject type and how the mode of that type (e.g.numeric, character,logical, etc.) will determine how R will operate on that data.
- Working with vectors effectively prepares you for understanding howdata are organized in R.