R Snippets

How to combine several date columns into one: weather <- data.frame(Year.UTC=c(2012, 2013), Month.UTC=c(1,2), Day.UTC=c(1,2), Hour.UTC=c(22,23)) weather <- within(weather, datetime <- as.POSIXct(paste(Year.UTC, Month.UTC, Day.UTC, Hour.UTC, sep=”-“), format=”%Y-%m-%d-%H”, tz=”UTC”)) Link Plot multiple histograms in vertical stack based on a factor library(ggplot2) library(ggplot2) ggplot(movies, aes(x = rating)) geom_histogram(binwidth = 0.5, aes(fill =factor(Comedy))) + facet_grid(Comedy~.) Link Check the column … [Read more…]

R and the apply functions

Link to useful site Lappy “lapply returns a list of the same length as X, each element of which is the result of applying FUN to the corresponding element of X.” 1 lapply(iris, mean)lapply(iris, mean) Vapply If all of your data is numeric, then this function will apply multiple functions to multiple columns simultaneously 1 … [Read more…]

R – find the mean of a column of values

1 2 3 # in a large data base (bnames) calculate a #mean of the values in the first 10 rows of the 3rd column: mean(bnames[1:10,3])# in a large data base (bnames) calculate a #mean of the values in the first 10 rows of the 3rd column: mean(bnames[1:10,3])

R Tutorials

Exclude missing variables from analysis 1 2 3 x <- c(5,7,8,NA,5) mean(x) # is NA mean (x, na.rm=TRUE) # returns correct answerx <- c(5,7,8,NA,5) mean(x) # is NA mean (x, na.rm=TRUE) # returns correct answer Recode a variable 1 2 # Select rows where age is 35 and change it to 55 mydata[mydata$age==35, "age"] <-55# … [Read more…]