data.frame
s and tibble
s by making them with data.frame()
or tibble()
, or reading in data with data()
or read.csv
$
filter
and select
for
looptable()
command is super handy for thistable(data)
## data
## Heads Tails
## 253 247
prop.table(table(data))
## data
## Heads Tails
## 0.506 0.494
mean(x)
x <- c(1,2,2,3,4)
mean(x)
## [1] 2.4
1*(1/5)+2*(2/5)+3*(1/5)+4*(1/5)
1*(1/5)+2*(1/5)+2*(1/5)+3*(1/5)+4*(1/5)
## [1] 2.4
x-mean(x)
is 0mean(c(1,2,3))
is 2
, but mean(c(1,2,1001))
is 334.6666667x <- c(3,1,4,2,2)
median(x)
## [1] 2
sort(x)[round(length(x)/2)]
## [1] 2
median(c(1,2,3))
is 2
, and median(c(1,2,1001))
is 2quantile()
function, and list the percentiles you wantquantile(c(0,1,2,3,4,5),c(.4,.5,1))
## 40% 50% 100%
## 2.0 2.5 5.0
median(c(0,1,2,3,4,5))
## [1] 2.5
min()
and max()
work here, no surprises!data <- c(1,1,1,1,2)
data <- data - mean(data)
data
## [1] -0.2 -0.2 -0.2 -0.2 0.8
#Variance, sd
c((5/4)*mean(data^2),var(c(1,1,1,1,2)),
sqrt((5/4)*mean(data^2)),sd(c(1,1,1,1,2)))
## [1] 0.2000000 0.2000000 0.4472136 0.4472136
data2 <- c(100,0,-30,50,80)
data2 <- data2 - mean(data2)
#Variance, sd
c((5/4)*mean(data2^2),var(c(100,0,-30,50,80)),
sqrt((5/4)*mean(data2^2)),sd(c(100,0,-30,50,80)))
## [1] 2950.0000 2950.0000 54.3139 54.3139
stargazer
command for this##
## ===================================================================
## Statistic N Mean St. Dev. Min Pctl(25) Pctl(75) Max
## -------------------------------------------------------------------
## sr 50 9.671 4.480 0.600 6.970 12.617 21.100
## pop15 50 35.090 9.152 21.440 26.215 44.065 47.640
## pop75 50 2.293 1.291 0.560 1.125 3.325 4.700
## dpi 50 1,106.758 990.869 88.940 288.207 1,795.622 4,001.890
## ddpi 50 3.758 2.870 0.220 2.002 4.477 16.710
## -------------------------------------------------------------------
install.packages('nameofpackage')
install.packages('stargazer')
select
first if you don’t want all the variables)data(LifeCycleSavings)
library(stargazer)
stargazer(LifeCycleSavings,type='text')
##
## ===================================================================
## Statistic N Mean St. Dev. Min Pctl(25) Pctl(75) Max
## -------------------------------------------------------------------
## sr 50 9.671 4.480 0.600 6.970 12.617 21.100
## pop15 50 35.090 9.152 21.440 26.215 44.065 47.640
## pop75 50 2.293 1.291 0.560 1.125 3.325 4.700
## dpi 50 1,106.758 990.869 88.940 288.207 1,795.622 4,001.890
## ddpi 50 3.758 2.870 0.220 2.002 4.477 16.710
## -------------------------------------------------------------------
type='text'
tells it to give us a basic text table.type='html'
, especially if we want to output our table to a fileout='filename'
will save our resultsdata(LifeCycleSavings)
library(stargazer)
stargazer(LifeCycleSavings,type='html',out='summarytable.html')
tibble
s, so if you have a tibble, just run it through as.data.frame()
firsttibbleLCS <- as_tibble(LifeCycleSavings)
stargazer(tibbleLCS,type='text')
##
## ===================================================
## Statistic N Mean St. Dev. Min Pctl(25) Pctl(75) Max
## ===================================================
stargazer(as.data.frame(tibbleLCS),type='text')
##
## ===================================================================
## Statistic N Mean St. Dev. Min Pctl(25) Pctl(75) Max
## -------------------------------------------------------------------
## sr 50 9.671 4.480 0.600 6.970 12.617 21.100
## pop15 50 35.090 9.152 21.440 26.215 44.065 47.640
## pop75 50 2.293 1.291 0.560 1.125 3.325 4.700
## dpi 50 1,106.758 990.869 88.940 288.207 1,795.622 4,001.890
## ddpi 50 3.758 2.870 0.220 2.002 4.477 16.710
## -------------------------------------------------------------------
stargazer
data(LifeCycleSavings)
to get the Life Cycle Savings data, and use help()
and str()
to look at itstargazer()
to get a text table of summary statistics for all the variables EXCEPT ddpistargazer()
table gives you, plus the median, calculate that statistic on your own for the pop15
variable using the appropriate R functioninstall.packages('stargazer')
library(stargazer)
data(LifeCycleSavings)
help(LifeCycleSavings)
str(LifeCycleSavings)
stargazer(select(LifeCycleSavings,-ddpi),type='text')
stargazer(select(LifeCycleSavings,-ddpi),type='html',out='table.html')
LS <- LifeCycleSavings
c(length(LS$pop15),mean(LS$pop15),sd(LS$pop15),min(LS$pop15),
quantile(LS$pop15,c(0,.25,.5,.75,1)),max(LS$pop15),median(LS$pop15))