Sunday, March 8, 2015

R Commands

Opening a CSV File

USDA = read.csv("USDA.csv")

Statistical functions

summary (USDA)

Getting a vector from data frame

dataframe$vectorname

Dataframe operations

dataframename = data.frame(vector1, vector2)
dataframe$newvector = newvector
subset (dataframe, condition) e.g. subset (USDA, sodium > 10000)
rbind(dataframe1, dataframe2)

nrow (dataframe)
names (USDA)
str(USDA)

merge(targetdataframe, sourcedataframe, by.x="fieldname1", by.y="fieldname2", all.x=TRUE)
by.x =
by.y =
all.x=TRUE means we want to keep all rows from the "x" data frame (CPS), even if some of the rows' MetroAreaCode doesn't match any codes in MetroAreaMap

Vector operations
A vector is a series of numbers or characters stored as the same object.

c(2,3,5,8,13) - combine function creates a vector of 5 numbers
which.min (vectorname)
which.max (vectorname)
match (valuetomatch, vectorname)
mean(vectorname, na.rm = TRUE)
sd(vectorname, na.rm = TRUE)
str (vectorname)

Plots
plot(x-axis-vectorname, y-axis-vectorname)
plot(x-axis-vectorname, y-axis-vectorname, xlab = "X Axis Label", ylab = "Y Axis Label", main= "Graph Title", col = "Color of Graph")
hist(vectorname)
hist(vectorname, xlab = "X Axis Label")
hist(vectorname, xlab = "X Axis Label",  main= "Graph Title")
hist(vectorname, xlab = "X Axis Label",  main= "Graph Title", xlim = c(0,100))
hist(vectorname, xlab = "X Axis Label",  main= "Graph Title", xlim = c(0,100), breaks = 2000)
boxplot(vectorname)

Table
table (vectorname)
table (vector1, vector2)
tapply (argument1, argument2, argument3) = Group argument1 by argument2 and apply argument3

e.g. tapply(IBM$StockPrice, months(IBM$Date), mean)
e.g. tapply(is.na(CPS$MetroAreaCode), CPS$State, mean)


Misc
seq (0,100,2)

Missing Values
is.na(vectorname)



No comments:

Post a Comment