forked from joanby/r-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-load-data.Rmd
More file actions
112 lines (93 loc) · 2.61 KB
/
Copy path02-load-data.Rmd
File metadata and controls
112 lines (93 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
---
title: "Carga de DF"
author: "Curso de Estadística Descriptiva"
date: "23/12/2018"
output: html_document
---
# Carga de ficheros local
Breed (1/5/8),
Sale Price,
Yearling height at shoulder (in.),
Fat Free Body (lbs.),
Percent Fat-free body,
Frame -- scale from 1 (small) to 8 (large),
Back fat (in.),
sale height at shoulder (in.)
sale weight (lbs.) of three breeds of bulls.
```{r}
df = read.table("../../data/bulls.dat",
header = FALSE,
col.names = c("breed", "sale_price", "shoulder",
"fat_free", "percent_ff", "frame_scale",
"back_fat", "sale_height", "sale_weight"),
sep = "", dec = ".")
head(df)
```
# Carga desde URL
```{r}
df2 = read.table("https://maitra.public.iastate.edu/stat501/datasets/bulls.dat",
header = FALSE,
col.names = c("breed", "sale_price", "shoulder",
"fat_free", "percent_ff", "frame_scale",
"back_fat", "sale_height", "sale_weight"),
sep = "", dec = ".")
head(df2)
str(df2)
```
# Factores en un DF
```{r}
df3 = read.table("https://maitra.public.iastate.edu/stat501/datasets/olive.dat",
stringsAsFactors = FALSE,
header = TRUE)
str(df3)
```
# Guardar un DF
```{r}
write.table(df3, file = "../../data/olive.txt", dec = ".")
df4 = read.table("../../data/olive.txt", header = TRUE, dec = ".")
head(df4)
```
# Crear un DF
```{r}
gender = c("H", "M", "M", "M", "H")
age = c( 23, 45, 20, 30, 18)
family = c( 2, 3, 4, 2, 5)
df5 = data.frame(genero = gender, edad = age, familia = family, stringsAsFactors = TRUE)
row.names(df5) = c("P1", "P2", "P3", "P4", "P5")
df5
str(df5)
dimnames(df5) = list(
c("Antonio", "Ricardo", "JuanGabriel", "María", "Margarita"),
c("Sexo", "Años", "MiembrosFamilia")
)
df5
df5 = rbind(df5, c("H", 30, 1))
df5
df5$Sexo = as.character(df5$Sexo)
df5$Ingresos = c(10000, 12000, 15000, 12000, 20000, 10000)
```
```{r}
gender = c("H", "M", "M", "M", "H")
age = c( 23, 45, 20, 30, 18)
family = c( 2, 3, 4, 2, 5)
df5 = data.frame(genero = gender, edad = age, familia = family, stringsAsFactors = TRUE)
df5[df5$genero=="M", ] -> df_m
str(df_m)
df_m = droplevels(df_m)
str(df_m)
```
# Tidyverse
```{r}
library(tidyverse)
iris_petal = select(iris, starts_with("Petal"))
head(iris_petal)
iris_length = select(iris, ends_with("Length"))
head(iris_length)
```
# Subset
```{r}
subset(iris, Species == "versicolor", select = c(1,3)) -> versicolor
rownames(versicolor) = 1:nrow(versicolor)
head(versicolor, 5)
str(versicolor)
```