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() {
using Plots | |
# a vector of r values | |
Rs=collect(0.1:0.001:3) | |
T = 5000 | |
N=zeros(length(Rs), T) | |
#Set t0 values to 1 | |
N[:,1] .= 1 |
library(shiny) | |
library(bslib) | |
library(thematic) | |
library(ggplot2) | |
thematic::thematic_shiny(font = "auto") | |
# take a vector, make a histogram |
library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.0.2
#> Warning: package 'tidyr' was built under R version 4.0.2
#> Warning: package 'dplyr' was built under R version 4.0.2
tibble(x = 0:42,
y1 = pbinom(x, 42, 0.3),
y2 = 1-pbeta(0.3, x+1, 42-x)) %>%
ggplot(aes(x = x, y = y1)) +
geom_point(col = "green", size = 3) +
--- | |
title: "Exploring observations" | |
author: "Andrew, based on Tim's lecture!" | |
date: "18/05/2020" | |
output: html_document | |
--- | |
```{r setup, include=FALSE} | |
knitr::opts_chunk$set(echo = TRUE) | |
``` |
m <- 0.086 | |
o <- (1-m)/m | |
curve((sqrt(o) - sqrt(o^-1)) / sqrt(x^2 - x + 1), xlim = c(0,50)) | |
abline(h = 1/3) |
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)
library(tidyverse)
cv <- 2.5
sigma = seq(0.2,5,by = 0.2)
list(mean = sigma / cv, sd = sigma) %>% transpose %>%
set_names(., nm = map_chr(., lift_dl(paste, sep="_"))) %>%
map(lift_dl(partial, .f = dnorm)) %>%
map_df(~ tibble(x = seq(-2,3.5,0.01),
y = .x(x)), .id = "mean_sd") %>%
ggplot(aes(x = x, ymax = y, ymin = 0, fill = mean_sd)) + geom_ribbon() +
pwr <- function(S, a, mu = 0.086, phi=240){ | |
D <- S * (S - 1) | |
2 * (D + 1) * (D/(phi + 1) + 1) * mu * ( 1 - mu) + ( a * mu + mu) * (1 - a * mu - mu) * (2.8)^2 / ((D + 1)*a*mu)^2 | |
} | |
pwr(6, 0.10) | |
curve(pwr(x, 0.4), xlim = c(3,30)) | |
curve(pwr(x, 0.2), xlim = c(3,30), log = "y") |
make_plot <- function(mydata){ | |
qplot(Length, Freq, data=mydata) | |
} |