Quotes help make search much faster. Example: "Practice Makes Perfect"

Showing posts with label R programming. Show all posts
Showing posts with label R programming. Show all posts

Tuesday, October 22, 2013

In R, how do you test if the correlation between two vectors is statistically significant?

Answer:

cor.test(VectorA, VectorB)

// this will display a report showing the p-value
// a p-value less than 0.05 is considered statistically significant

In R, how do you read the contents of a .txt file into a data frame?

Answer:

someDataframe <- read.table("someFile.txt", sep="\t", header=TRUE)

//in this example, the columns in the .txt file are separated by tabs (tab delimited)
//header = TRUE indicates that the first row in the .txt file represents the headers of the data frame
//change this to FALSE if the first row already represents raw data

In R, how do you store the contents of a .csv file into a data frame?

Answer:

someDataframe <- read.csv("someFile.csv")

In R, how do you determine the unique elements in a vector?

Answer:

someFactor <- factor(someVector)
//convert the vector to a factor using the factor function

levels(someFactor)
//use the levels function to identify the unique elements in the vector

In R, how do you compute for the standard deviation of a vector composed of numbers, numVector?

Answer:

sd(numVector)

In R, what are the ways you can plot a matrix someMatrix?

Possible answers:

contour(someMatrix)

persp(someMatrix, expand=0.2)
//this creates a 3D-plot where the z-axis is dependent on the values of the elements of the matrix

image(someMatrix)
//this creates a heat map of the matrix, kind of a colored version of contour

In R, how do you access an element in a matrix someMatrix?

Answer:

someMatrix[rowIndex, columnIndex]

In R, how can you re-shape a vector?

Answer:
dim(someVector) <- c(numRows, numCols)

//note, the length of someVector must be equal to the product of numRows and numCols

In R, how do you create an n row by m column matrix?

Answer:

matrix(elementValues, n, m)

//this will create an n x m matrix where all elements have a value of elementValues

//if elementValues = 0 , all the elements of the n x m matrix will be 0
//if elementValues is a vector of length equal to the product of n and m, then the matrix will contain all the elements of the vector populated from top to bottom, then left to right

In R, how do you neglect elements with an assigned value NA in a vector when it is used in someFunction?

Answer:

//use
help(someFunc)

//if someFunc has the provision, na.rm = FALSE, then

someFunc(someVector, na.rm = TRUE)

//this will neglect the elements in the vector someVector with NA values

In R, how do you create a scatter plot given two vectors, x and y?

Answer:

plot(x, y)

//where the x vector will represent the x-axis and the y vector will represent the y-axis

In R, how do you access the nth element in a vector someVector?

Answer:
someVector[n]

//note: unlike most programming languages where array indices are zero-based, in R, the first element's index is 1

In R, how could you make a vector filled with values from 1 to 50?

Answer:

Method 1

c(1:50)

//note, this uses the c (combine) function and start:end notation

Method 2

seq(1,50)

//note, this uses the seq function, and instead of a colon (:), a comma is placed between the start and end numbers

In R, what happens when you try to create a vector with different types (modes)?

Answer:
R will convert all values to a single mode, usually to a string, since a vector's elements need to be of the same type (mode)

In R, how do you run a script names someScript?

Answer:
source("someScript")

In R, how can you list files in the current working directory?

Answer:
list.files()

In R, how do you find help on a function?

Answer:
help(functionName)

example:
help(sqrt)

//will bring up the R documentation on the sqrt function

In R, wow do you store a value in a variable someVar?

someVar <- 7

// this assigns the value 7 to someVar

someVar <- "Hello World!"

//this assigns a string to someVar

someVar <- TRUE

//this assigns the logical value of TRUE to someVar

In R, what is the shorthand for FALSE?

Answer: F

In R, what is the shorthand for TRUE?

Answer: T

This is an example of scrolling text using Javascript.

Popular Posts