Skip to content

Instantly share code, notes, and snippets.

@yabyzq
Created October 24, 2016 13:14
Show Gist options
  • Save yabyzq/6cbb5634092ae3930ee735f5a8c3df37 to your computer and use it in GitHub Desktop.
Save yabyzq/6cbb5634092ae3930ee735f5a8c3df37 to your computer and use it in GitHub Desktop.
numeric and factor basic script
#1 Numeric Variable
numeric_stats <- function (x, na.omit =FALSE){
if(na.omit)
x <-x[!is.na(x)]
m <- mean(x)
a <- median(x)
s <- sd(x)
min <- min(x)
max <- max(x)
return(c(min = min, mean = m, avg = a, max = max, sd = s))
}
t(sapply(mtcars, numeric_stats))
#2 Numberical Variable Group by target
aggregate(mtcars, by = list(am=mtcars$am), median)
library(doBy)
t(summaryBy(mpg+hp+wt~am, data = mtcars, FUN = numeric_stats))
#3 Categorical Variable
table(iris$Species)
prop.table(table(iris$Species))*100
prop.table(xtabs(~ factor(am) + factor(cyl) , data =mtcars),1)
prop.table(xtabs(~ factor(am) + factor(cyl), data =mtcars),2)
chisq.test(xtabs(~ factor(am) + factor(cyl), data =mtcars))
fisher.test(xtabs(~ factor(am) + factor(cyl), data =mtcars))
library(vcd)
assocstats(xtabs(~ factor(am) + factor(cyl), data =mtcars))
#4 Correlation
cor(mtcars)
cor(mtcars, method = "spearman")
#5 T
t.test(cyl ~ factor(am), data= mtcars)#anova
library(psych)
principal(iris[,1:4],nfactors =2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment