#Para aprender sobre R, um começo é digitar help.start() na linha de comando do R
#Pequena introdução:
#data types:
a = c(1,2,5.3,6,-2,4) # numeric vector
b = c(“one”,”two”,”three”) # character vector
c = c(TRUE,TRUE,TRUE,FALSE,TRUE,FALSE) #logical vector
#generates 5 x 4 numeric matrix
y = matrix(1:20, nrow=5,ncol=4)
#size, number of rows and of columns:
dim(y); dim(y)[1]; dim(y)[2]
#Select data
y[1,1] # one value
y[1,] # first row
y[-1,] # all but first row
y[c(2,4)] # 2nd and 4th elements of vector
#input data:
age = c(43, 30, 40)
gender = c(“male”, “female”, “female”)
weight = c(70, 100, 50)
data = data.frame(age,gender,weight)
#import data:
data = read.table(‘g:/r/data.txt’, header=TRUE, sep=”)
#mean, median, 25th and 75th quartiles, min, max:
summary(data, digits=2)
#Visualize data:
boxplot(data)
plot(x,y, xlab=”x axis”, ylab=”y axis”, main=”my plot”, ylim=c(0,20), xlim=c(0,20), pch=15, col=”blue”)
%plot all columns in differents plots at the same window:
par(mfrow = c(dim(data)[2]-1,1)) # windows() for a new window
for (i in 1:(dim(data)[2]-1))
plot(data[,1],data[,i+1],xlab=names(data)[1],ylab=names(data)[i+1])
#univariate normality:
library(nortest)
ad.test(data[,2])
#homegeneity of variances:
bartlett.test(y~G, data=data) #y is numeric and G is the grouping variable
plot(count ~ spray, data = InsectSprays)
bartlett.test(InsectSprays$count, InsectSprays$spray)
bartlett.test(count ~ spray, data = InsectSprays)
#t-tests:
#independent 2-group t-test
t.test(y1,y2) # y1 and y2 are numeric
#paired t-test
t.test(y1,y2,paired=TRUE) # y1 & y2 are numeric
#one samle t-test
t.test(y,mu=3) # Ho: mu=3
#nonparametric Tests of Group Differences:
# independent 2-group Mann-Whitney U Test
wilcox.test(y1,y2) # y and x are numeric
# dependent 2-group Wilcoxon Signed Rank Test
wilcox.test(y1,y2,paired=TRUE) # y1 and y2 are numeric
#Control Structures
#if else
if (cond) expr
if (cond) expr1 else expr2
#for
for (var in seq) expr
#while
while (cond) expr
#switch
switch(expr, …)
#function
rms = function(x) {
y = sqrt(sum(x^2)/length(x))
return(y)
}