Is there an easy way to convert a named list into a dataframe, preserving the elements of the list in a "list-column"?
library(dplyr)
library(magrittr)
## make a random matrix
rand_mat <- function() {
Is there an easy way to convert a named list into a dataframe, preserving the elements of the list in a "list-column"?
library(dplyr)
library(magrittr)
## make a random matrix
rand_mat <- function() {
Apparently, there is a simple problem called Fizz buzz which is sometimes used to identify competent programmers. A good opportunity to practice some dplyr
and magrittr
tricks.
library(dplyr)
library(magrittr)
library(knitr)
1:100 %>%
data.frame %>%
Many years ago, I was introduced to R by Cam Webb . At the time, his website contained a list of common data manipulations (original here). This list dated from Cam's early experience with R, and contained the R-help mailing list responses to a series of data manipulations. For a long time, I kept this file as a handy reference. I printed it out. I recommended it to friends.
Now I have been using R for years, and the state of the art has advanced considerably. Particulary, Hadley Wickham's reshape2
and dplyr
packages have transformed the way most useRs manipulate their data. I decided that it would be interesting to revisit my favourite resource and try my hand at solving these problems with tools from these two packages.
library(reshape2)
library(dplyr)
## a demonstration of ordered contrasts | |
## three levels, A, B, and C | |
factor1 <- rep(LETTERS[1:3],c(15,15,15)) | |
## Another factor | |
factor2 <- rep(letters[1:3],15) | |
## check combinations | |
table(factor1,factor2) |
## make a fake data frame | |
test <- data.frame(foo=gl(n=3,k=2,labels=c("low","med","high")),bar=rep(letters[1:2],3),zoop=runif(6)) | |
## these are all factors | |
str(test) | |
## I want them to be characters, not factors, but I am too lazy to run as.character on every categorical factor | |
library(plyr) | |
test_char <- catcolwise(as.character)(test) |
eps <- function(file, onefile=FALSE, width=5.5, height = 5.5, horizontal=FALSE,paper="special", ...){ | |
postscript(file=file,width=width,height=height,onefile=onefile,horizontal=horizontal,paper=paper,title=file,...) | |
par(bty='l') | |
} |
x1 <- rnorm(100,5,sd=2)
x2 <- rnorm(200,15,3)
x3 <- c(x1,x2)
## calculate the density function
den_x <- density(x3)]
## this can be plotted:
plot(den_x)
getmatch <<- function(x,str2match,...) { | |
unlist(regmatches(x,regexpr(str2match,x,...))) } |
library(lme4) | |
library(ggplot2) | |
#create some levels | |
levs <- as.factor(c("l1","l2","l3","l4","l5")) | |
#set the factor means | |
f_means <- c(6,16,2,10,13) | |
# set individual as a factor |
## file-scan.r | |
## Function to find a pattern in files with user-defined extension (.r by default) | |
## Returns file name and approximate row matches if positive. | |
## ------------------------------------------------------- | |
## Author: Laura Tremblay-Boyer ([email protected]) | |
## Written on: March 26, 2013 | |
## Time-stamp: <2013-03-26 16:19:28 Laura> | |
file.scan <- function(pattern, dir=getwd(), fpattern=NA, ftype=".r") { |