a=c(1,2,3,4)
b=matrix(a,2,2,byrow=FALSE)
b
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
#d=edit(b)
#d
#data.entry(x2=c(NA))
#data.entry(x3=c(""))
#x=edit(as.data.frame(NULL))

x=c(1,2)
y=c("a","b")
#data.entry(x)
#data.entry()
peso=c(75,98,67,80)
altura=c(1.75,1.89,1.61,1.77)
sexo=c("F","F","M","F")
personas=data.frame(peso,altura,sexo)
personas
##   peso altura sexo
## 1   75   1.75    F
## 2   98   1.89    F
## 3   67   1.61    M
## 4   80   1.77    F
rm(peso);rm(altura);rm(sexo)
row.names(personas)=c("Maria","Alicia","Pedro","Judith")
personas
##        peso altura sexo
## Maria    75   1.75    F
## Alicia   98   1.89    F
## Pedro    67   1.61    M
## Judith   80   1.77    F
df=data.frame(c(1,2,3),c(7,3,4))
df
##   c.1..2..3. c.7..3..4.
## 1          1          7
## 2          2          3
## 3          3          4
df=data.frame(col1=c(1,2,3),col2=c(7,3,4))
df
##   col1 col2
## 1    1    7
## 2    2    3
## 3    3    4
df$col1
## [1] 1 2 3
df[,1]
## [1] 1 2 3
df[["col1"]]
## [1] 1 2 3
personas[2]
##        altura
## Maria    1.75
## Alicia   1.89
## Pedro    1.61
## Judith   1.77
personas[,2]
## [1] 1.75 1.89 1.61 1.77
personas["Maria","altura"]
## [1] 1.75
personas$altura
## [1] 1.75 1.89 1.61 1.77
dim(df)
## [1] 3 2
names(df)
## [1] "col1" "col2"
row.names(df)
## [1] "1" "2" "3"
attach(df)
col1
## [1] 1 2 3
detach(df)

Filtros

personas[personas$sexo=='F',]
##        peso altura sexo
## Maria    75   1.75    F
## Alicia   98   1.89    F
## Judith   80   1.77    F
personas[personas$sexo=='F' & personas$altura>1.75,]
##        peso altura sexo
## Alicia   98   1.89    F
## Judith   80   1.77    F
attach(personas)
personas[sexo=='F' & altura>1.75,]
##        peso altura sexo
## Alicia   98   1.89    F
## Judith   80   1.77    F
detach(personas)

Ocurrencias

attach(personas)
table(sexo)
## sexo
## F M 
## 3 1
table(sexo,peso)
##     peso
## sexo 67 75 80 98
##    F  0  1  1  1
##    M  1  0  0  0

Cruces

x=data.frame(id=c(1,2,3,4), datos=c(70,30,40,100))
y=data.frame(id=c(1,2,3,5),datos=c(.5,.3,.2,.1))
merge(x,y,by=("id"))
##   id datos.x datos.y
## 1  1      70     0.5
## 2  2      30     0.3
## 3  3      40     0.2
merge(x,y,by=("id"),all.x=TRUE)
##   id datos.x datos.y
## 1  1      70     0.5
## 2  2      30     0.3
## 3  3      40     0.2
## 4  4     100      NA
merge(x,y,by=("id"),all=TRUE)
##   id datos.x datos.y
## 1  1      70     0.5
## 2  2      30     0.3
## 3  3      40     0.2
## 4  4     100      NA
## 5  5      NA     0.1

Funciones

z=data.frame(id=c(1,2,2,4,5,4), datos1=c(70,30,40,100,22,33), datos2=runif(6))
aggregate(z[,2:length(z)],by=list(z$id), FUN=sum)
##   Group.1 datos1    datos2
## 1       1     70 0.1621190
## 2       2     70 0.9047758
## 3       4    133 0.8816919
## 4       5     22 0.3009802
apply(z,1,FUN=mean)
## [1] 23.720706 10.817392 14.150866 34.763606  9.100327 12.530292
apply(z,2,FUN=mean)
##         id     datos1     datos2 
##  3.0000000 49.1666667  0.3749278

Listas

h=list()
h
## list()
h[["a"]]=1
h
## $a
## [1] 1
h[["b"]]=c(1,2,3)
h
## $a
## [1] 1
## 
## $b
## [1] 1 2 3
h[["a"]]=NULL
h
## $b
## [1] 1 2 3

Exportar

df=data.frame(runif(10),runif(10),runif(10))
names(df)=c("dato1","dato2","dato3")
write.table(df,file="dataframe1.csv", sep=",",col.names=NA, qmethod="double")
write.table(df,file="dataframe2.csv", sep=",",row.names=FALSE, qmethod="double")

Importar

read.table("dataframe1.csv",header = TRUE, sep=",",row.names=1)
##         dato1      dato2     dato3
## 1  0.86553401 0.87171708 0.4690772
## 2  0.01466934 0.86194461 0.7846525
## 3  0.76865375 0.94876216 0.6352314
## 4  0.73527763 0.90467268 0.7210262
## 5  0.75325026 0.19734813 0.7592071
## 6  0.74078908 0.04286996 0.9994816
## 7  0.47032248 0.42355848 0.3401037
## 8  0.45902410 0.69223626 0.8045361
## 9  0.29403547 0.34146005 0.3235633
## 10 0.63892904 0.08827577 0.6800554
read.table("dataframe1.csv",header = TRUE, sep=",")
##     X      dato1      dato2     dato3
## 1   1 0.86553401 0.87171708 0.4690772
## 2   2 0.01466934 0.86194461 0.7846525
## 3   3 0.76865375 0.94876216 0.6352314
## 4   4 0.73527763 0.90467268 0.7210262
## 5   5 0.75325026 0.19734813 0.7592071
## 6   6 0.74078908 0.04286996 0.9994816
## 7   7 0.47032248 0.42355848 0.3401037
## 8   8 0.45902410 0.69223626 0.8045361
## 9   9 0.29403547 0.34146005 0.3235633
## 10 10 0.63892904 0.08827577 0.6800554

Visualizacion

gastos=c(gimnasio=60,Dietas=345, Alquiler=800, Transporte=200)
pie(gastos)

barplot(gastos)

transferencias=abs(rnorm(100, mean=100, sd=5))
hist(transferencias)

hist(transferencias, probability = TRUE)

hist(transferencias)
rug(transferencias)

hist(transferencias, probability = TRUE)
lines(density(transferencias), col="blue", lwd=3)

transferencias.nacionales=abs(rnorm(100,mean=130, sd=50))
transferencias.internacionales=abs(rnorm(100,mean=150, sd=60))
transferencias.nacionales[1]=310
transferencias.nacionales[2]=280
transferencias.internacionales[1]=350
boxplot(transferencias.nacionales,transferencias.internacionales, names=c("nacionales", "internacionales"), horizontal=TRUE)

boxplot(transferencias.nacionales,transferencias.internacionales, names=c("nacionales", "internacionales"))

barplot(table(sexo,peso), main="cruce de sexo vs peso", beside=TRUE,legend.text = c("F","M"))