Skip to content

Lesson 2: Help Session

This is our first coding help session. We have designed some practice problems to get you acquainted with using R before beginning to wrangle in our next lesson.

Practice problems

Which of the following will throw an error and why?

4_chr <- c('chr13', 'chr4','chr9','chr01')
chr.4 <- c('chr13', 'chr4','chr9','chr01')
.4chr <- c('chr13', 'chr4','chr9','chr01')
chr_4 <- c('chr13', 'chr4','chr9','chr01')
chr4 <- c('chr13', 'chr4','chr9','chr01')

Solution}

4_chr <- c('chr13', 'chr4','chr9','chr01')
chr.4 <- c('chr13', 'chr4','chr9','chr01')
## Error: <text>:1:2: unexpected input
## 1: 4_
##      ^
.4chr <- c('chr13', 'chr4','chr9','chr01')
chr_4 <- c('chr13', 'chr4','chr9','chr01')
chr4 <- c('chr13', 'chr4','chr9','chr01')

## Error: <text>:1:3: unexpected symbol
## 1: .4chr
##       ^

Create the following objects; give each object an appropriate name (your best guess at what name to use is fine):

  1. Create an object that has a value of your favorite gene name
  2. Create an object containing a vector of numbers 8-16.
  3. Create an object containing a vector of numbers 22-29.
  4. Combine the vectors from question 4 and 5 and save to a new object.

Solution}

(fav_gene<-"GH1")
## [1] "GH1"
(vec1<-c(8:16))
## [1]  8  9 10 11 12 13 14 15 16
(vec2<-c(22:29))
## [1] 22 23 24 25 26 27 28 29
(vec3<-c(vec1,vec2))
##  [1]  8  9 10 11 12 13 14 15 16 22 23 24 25 26 27 28 29

Create the following objects in R. What type of data are stored in these objects?

chromosome_name <- 'chr02'
ODval <- 0.47
chr_position <- '1001701'
question <- TRUE
irregular <- NA

Solution}

typeof(chromosome_name)
## [1] "character"
typeof(ODval)
## [1] "double"
typeof(chr_position)
## [1] "character"
typeof(question)
## [1] "logical"
typeof(irregular)
## [1] "logical"

Here are some interesting vectors.

snps <- c('rs53576', 'rs1815739', 'rs6152', 'rs1799971')
snp_chromosomes <- c('3', '11', 'X', '6')
snp_positions <- c(8762685, 66560624, 67545785, 154039662)

If you combine, snp_positions with snp_chromosomes, what is the resulting data type?

Solution}

typeof(c(snp_positions,snp_chromosomes))

## [1] "character"

Add 23792 to the snp_positions vector and overwrite the object.

Solution}

(snp_positions <- c(snp_positions, 23792)) 

## [1]   8762685  66560624  67545785 154039662     23792

Use the following example data frame:

df<-data.frame(id=paste("Sample",1:10,sep="_"), cell=rep(factor(c("cell_line_A","cell_line_B")),each=10),counts=sample(1:1000,20,replace=TRUE))

\
\

What are the column names? Rename the column "id" to "Sampleid". [You will likely need to look up how to do this. How can you find help?]

Solution}

colnames(df)
## [1] "id"     "cell"   "counts"

colnames(df)[1] <- "Sampleid"

Save the column 'cell' from df to an object called 'cell_line'.

Solution}

cell_line <- df$cell
cell_line

##  [1] cell_line_A cell_line_A cell_line_A cell_line_A cell_line_A cell_line_A
##  [7] cell_line_A cell_line_A cell_line_A cell_line_A cell_line_B cell_line_B
## [13] cell_line_B cell_line_B cell_line_B cell_line_B cell_line_B cell_line_B
## [19] cell_line_B cell_line_B
## Levels: cell_line_A cell_line_B

Duplicate the column 'cell' and save to a new column of df called 'cell_line'.

Solution}

(df$cell_line <- df$cell)

##  [1] cell_line_A cell_line_A cell_line_A cell_line_A cell_line_A cell_line_A
##  [7] cell_line_A cell_line_A cell_line_A cell_line_A cell_line_B cell_line_B
## [13] cell_line_B cell_line_B cell_line_B cell_line_B cell_line_B cell_line_B
## [19] cell_line_B cell_line_B
## Levels: cell_line_A cell_line_B

Look at the help documentation for is.na(). Use this function to determine whether there are NAs in the following vector:

vec_a<-c(88, 347, 53, 27, 94, NA,28, 409, NA)

Solution}

is.na(vec_a) #returns logical vector
## [1] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE
which(is.na(vec_a)) #provides index or location in vector

## [1] 6 9

Acknowledgements

Questions were inspired by or taken directly from datacarpentry.org