Busca los datasets “beaver1” y “beaver2” que contienen información sobre la temperatura corporal de dos castores. Añade una columna llamada “ID” al dataset beaver1 que tenga siempre el valor 1. De forma similar añade una columna “ID” al dataset beaver2 que tenga siempre el valor 2. A continuación concatena de forma vertical los dos dataframes y busca el subset de datos donde ambos Castores están activos.
x<-cbind(beaver1,"ID"=1)
y<-cbind(beaver2,"ID"=2)
beavers<-rbind(x,y)
subb <- subset(beavers, beavers[,"activ"]==1)
nrow(subb)
## [1] 68
Vamos a trabajar con un ejemplo que viene por defecto en la instalación de R USArrests. Este data frame contiene la información para cada estado Americano de las tasas de criminales (por 100.000 habitantes). Los datos de las columnas se refieren a Asesinatos, violaciones yporcentaje de la población que vive en áreas urbanas. Los datos son de 1973. Contesta a las siguientes preguntas sobre los datos:
USArrests
## Murder Assault UrbanPop Rape
## Alabama 13.2 236 58 21.2
## Alaska 10.0 263 48 44.5
## Arizona 8.1 294 80 31.0
## Arkansas 8.8 190 50 19.5
## California 9.0 276 91 40.6
## Colorado 7.9 204 78 38.7
## Connecticut 3.3 110 77 11.1
## Delaware 5.9 238 72 15.8
## Florida 15.4 335 80 31.9
## Georgia 17.4 211 60 25.8
## Hawaii 5.3 46 83 20.2
## Idaho 2.6 120 54 14.2
## Illinois 10.4 249 83 24.0
## Indiana 7.2 113 65 21.0
## Iowa 2.2 56 57 11.3
## Kansas 6.0 115 66 18.0
## Kentucky 9.7 109 52 16.3
## Louisiana 15.4 249 66 22.2
## Maine 2.1 83 51 7.8
## Maryland 11.3 300 67 27.8
## Massachusetts 4.4 149 85 16.3
## Michigan 12.1 255 74 35.1
## Minnesota 2.7 72 66 14.9
## Mississippi 16.1 259 44 17.1
## Missouri 9.0 178 70 28.2
## Montana 6.0 109 53 16.4
## Nebraska 4.3 102 62 16.5
## Nevada 12.2 252 81 46.0
## New Hampshire 2.1 57 56 9.5
## New Jersey 7.4 159 89 18.8
## New Mexico 11.4 285 70 32.1
## New York 11.1 254 86 26.1
## North Carolina 13.0 337 45 16.1
## North Dakota 0.8 45 44 7.3
## Ohio 7.3 120 75 21.4
## Oklahoma 6.6 151 68 20.0
## Oregon 4.9 159 67 29.3
## Pennsylvania 6.3 106 72 14.9
## Rhode Island 3.4 174 87 8.3
## South Carolina 14.4 279 48 22.5
## South Dakota 3.8 86 45 12.8
## Tennessee 13.2 188 59 26.9
## Texas 12.7 201 80 25.5
## Utah 3.2 120 80 22.9
## Vermont 2.2 48 32 11.2
## Virginia 8.5 156 63 20.7
## Washington 4.0 145 73 26.2
## West Virginia 5.7 81 39 9.3
## Wisconsin 2.6 53 66 10.8
## Wyoming 6.8 161 60 15.6
dim.data.frame(USArrests) # 50 * 4
## [1] 50 4
nrow(USArrests) # 50
## [1] 50
ncol(USArrests) # 4
## [1] 4
nrow(USArrests)
## [1] 50
row.names(USArrests)
## [1] "Alabama" "Alaska" "Arizona" "Arkansas"
## [5] "California" "Colorado" "Connecticut" "Delaware"
## [9] "Florida" "Georgia" "Hawaii" "Idaho"
## [13] "Illinois" "Indiana" "Iowa" "Kansas"
## [17] "Kentucky" "Louisiana" "Maine" "Maryland"
## [21] "Massachusetts" "Michigan" "Minnesota" "Mississippi"
## [25] "Missouri" "Montana" "Nebraska" "Nevada"
## [29] "New Hampshire" "New Jersey" "New Mexico" "New York"
## [33] "North Carolina" "North Dakota" "Ohio" "Oklahoma"
## [37] "Oregon" "Pennsylvania" "Rhode Island" "South Carolina"
## [41] "South Dakota" "Tennessee" "Texas" "Utah"
## [45] "Vermont" "Virginia" "Washington" "West Virginia"
## [49] "Wisconsin" "Wyoming"
colnames(USArrests)
## [1] "Murder" "Assault" "UrbanPop" "Rape"
head(USArrests, 6)
## Murder Assault UrbanPop Rape
## Alabama 13.2 236 58 21.2
## Alaska 10.0 263 48 44.5
## Arizona 8.1 294 80 31.0
## Arkansas 8.8 190 50 19.5
## California 9.0 276 91 40.6
## Colorado 7.9 204 78 38.7
*Ordena de forma decreciente las filas de nuestro data frame según el porcentaje de población en el área urbana. Para ello investiga la función order () y sus parámetros.
USArrests[order(USArrests$UrbanPop, decreasing = TRUE),]
## Murder Assault UrbanPop Rape
## California 9.0 276 91 40.6
## New Jersey 7.4 159 89 18.8
## Rhode Island 3.4 174 87 8.3
## New York 11.1 254 86 26.1
## Massachusetts 4.4 149 85 16.3
## Hawaii 5.3 46 83 20.2
## Illinois 10.4 249 83 24.0
## Nevada 12.2 252 81 46.0
## Arizona 8.1 294 80 31.0
## Florida 15.4 335 80 31.9
## Texas 12.7 201 80 25.5
## Utah 3.2 120 80 22.9
## Colorado 7.9 204 78 38.7
## Connecticut 3.3 110 77 11.1
## Ohio 7.3 120 75 21.4
## Michigan 12.1 255 74 35.1
## Washington 4.0 145 73 26.2
## Delaware 5.9 238 72 15.8
## Pennsylvania 6.3 106 72 14.9
## Missouri 9.0 178 70 28.2
## New Mexico 11.4 285 70 32.1
## Oklahoma 6.6 151 68 20.0
## Maryland 11.3 300 67 27.8
## Oregon 4.9 159 67 29.3
## Kansas 6.0 115 66 18.0
## Louisiana 15.4 249 66 22.2
## Minnesota 2.7 72 66 14.9
## Wisconsin 2.6 53 66 10.8
## Indiana 7.2 113 65 21.0
## Virginia 8.5 156 63 20.7
## Nebraska 4.3 102 62 16.5
## Georgia 17.4 211 60 25.8
## Wyoming 6.8 161 60 15.6
## Tennessee 13.2 188 59 26.9
## Alabama 13.2 236 58 21.2
## Iowa 2.2 56 57 11.3
## New Hampshire 2.1 57 56 9.5
## Idaho 2.6 120 54 14.2
## Montana 6.0 109 53 16.4
## Kentucky 9.7 109 52 16.3
## Maine 2.1 83 51 7.8
## Arkansas 8.8 190 50 19.5
## Alaska 10.0 263 48 44.5
## South Carolina 14.4 279 48 22.5
## North Carolina 13.0 337 45 16.1
## South Dakota 3.8 86 45 12.8
## Mississippi 16.1 259 44 17.1
## North Dakota 0.8 45 44 7.3
## West Virginia 5.7 81 39 9.3
## Vermont 2.2 48 32 11.2
USArrests[order(c(USArrests$UrbanPop,USArrests$Assault), decreasing = TRUE),]
## Murder Assault UrbanPop Rape
## NA NA NA NA NA
## NA.1 NA NA NA NA
## NA.2 NA NA NA NA
## NA.3 NA NA NA NA
## NA.4 NA NA NA NA
## NA.5 NA NA NA NA
## NA.6 NA NA NA NA
## NA.7 NA NA NA NA
## NA.8 NA NA NA NA
## NA.9 NA NA NA NA
## NA.10 NA NA NA NA
## NA.11 NA NA NA NA
## NA.12 NA NA NA NA
## NA.13 NA NA NA NA
## NA.14 NA NA NA NA
## NA.15 NA NA NA NA
## NA.16 NA NA NA NA
## NA.17 NA NA NA NA
## NA.18 NA NA NA NA
## NA.19 NA NA NA NA
## NA.20 NA NA NA NA
## NA.21 NA NA NA NA
## NA.22 NA NA NA NA
## NA.23 NA NA NA NA
## NA.24 NA NA NA NA
## NA.25 NA NA NA NA
## NA.26 NA NA NA NA
## NA.27 NA NA NA NA
## NA.28 NA NA NA NA
## NA.29 NA NA NA NA
## NA.30 NA NA NA NA
## NA.31 NA NA NA NA
## NA.32 NA NA NA NA
## NA.33 NA NA NA NA
## NA.34 NA NA NA NA
## NA.35 NA NA NA NA
## NA.36 NA NA NA NA
## NA.37 NA NA NA NA
## NA.38 NA NA NA NA
## NA.39 NA NA NA NA
## California 9.0 276 91 40.6
## New Jersey 7.4 159 89 18.8
## Rhode Island 3.4 174 87 8.3
## New York 11.1 254 86 26.1
## NA.40 NA NA NA NA
## Massachusetts 4.4 149 85 16.3
## Hawaii 5.3 46 83 20.2
## Illinois 10.4 249 83 24.0
## NA.41 NA NA NA NA
## Nevada 12.2 252 81 46.0
## NA.42 NA NA NA NA
## Arizona 8.1 294 80 31.0
## Florida 15.4 335 80 31.9
## Texas 12.7 201 80 25.5
## Utah 3.2 120 80 22.9
## Colorado 7.9 204 78 38.7
## Connecticut 3.3 110 77 11.1
## Ohio 7.3 120 75 21.4
## Michigan 12.1 255 74 35.1
## Washington 4.0 145 73 26.2
## Delaware 5.9 238 72 15.8
## Pennsylvania 6.3 106 72 14.9
## NA.43 NA NA NA NA
## Missouri 9.0 178 70 28.2
## New Mexico 11.4 285 70 32.1
## Oklahoma 6.6 151 68 20.0
## Maryland 11.3 300 67 27.8
## Oregon 4.9 159 67 29.3
## Kansas 6.0 115 66 18.0
## Louisiana 15.4 249 66 22.2
## Minnesota 2.7 72 66 14.9
## Wisconsin 2.6 53 66 10.8
## Indiana 7.2 113 65 21.0
## Virginia 8.5 156 63 20.7
## Nebraska 4.3 102 62 16.5
## Georgia 17.4 211 60 25.8
## Wyoming 6.8 161 60 15.6
## Tennessee 13.2 188 59 26.9
## Alabama 13.2 236 58 21.2
## Iowa 2.2 56 57 11.3
## NA.44 NA NA NA NA
## New Hampshire 2.1 57 56 9.5
## NA.45 NA NA NA NA
## Idaho 2.6 120 54 14.2
## Montana 6.0 109 53 16.4
## NA.46 NA NA NA NA
## Kentucky 9.7 109 52 16.3
## Maine 2.1 83 51 7.8
## Arkansas 8.8 190 50 19.5
## Alaska 10.0 263 48 44.5
## South Carolina 14.4 279 48 22.5
## NA.47 NA NA NA NA
## NA.48 NA NA NA NA
## North Carolina 13.0 337 45 16.1
## South Dakota 3.8 86 45 12.8
## NA.49 NA NA NA NA
## Mississippi 16.1 259 44 17.1
## North Dakota 0.8 45 44 7.3
## West Virginia 5.7 81 39 9.3
## Vermont 2.2 48 32 11.2
USArrests$Murder
## [1] 13.2 10.0 8.1 8.8 9.0 7.9 3.3 5.9 15.4 17.4 5.3 2.6 10.4 7.2
## [15] 2.2 6.0 9.7 15.4 2.1 11.3 4.4 12.1 2.7 16.1 9.0 6.0 4.3 12.2
## [29] 2.1 7.4 11.4 11.1 13.0 0.8 7.3 6.6 4.9 6.3 3.4 14.4 3.8 13.2
## [43] 12.7 3.2 2.2 8.5 4.0 5.7 2.6 6.8
USArrests$Murder[2:4]
## [1] 10.0 8.1 8.8
USArrests[1:5,]
## Murder Assault UrbanPop Rape
## Alabama 13.2 236 58 21.2
## Alaska 10.0 263 48 44.5
## Arizona 8.1 294 80 31.0
## Arkansas 8.8 190 50 19.5
## California 9.0 276 91 40.6
USArrests[,1:2]
## Murder Assault
## Alabama 13.2 236
## Alaska 10.0 263
## Arizona 8.1 294
## Arkansas 8.8 190
## California 9.0 276
## Colorado 7.9 204
## Connecticut 3.3 110
## Delaware 5.9 238
## Florida 15.4 335
## Georgia 17.4 211
## Hawaii 5.3 46
## Idaho 2.6 120
## Illinois 10.4 249
## Indiana 7.2 113
## Iowa 2.2 56
## Kansas 6.0 115
## Kentucky 9.7 109
## Louisiana 15.4 249
## Maine 2.1 83
## Maryland 11.3 300
## Massachusetts 4.4 149
## Michigan 12.1 255
## Minnesota 2.7 72
## Mississippi 16.1 259
## Missouri 9.0 178
## Montana 6.0 109
## Nebraska 4.3 102
## Nevada 12.2 252
## New Hampshire 2.1 57
## New Jersey 7.4 159
## New Mexico 11.4 285
## New York 11.1 254
## North Carolina 13.0 337
## North Dakota 0.8 45
## Ohio 7.3 120
## Oklahoma 6.6 151
## Oregon 4.9 159
## Pennsylvania 6.3 106
## Rhode Island 3.4 174
## South Carolina 14.4 279
## South Dakota 3.8 86
## Tennessee 13.2 188
## Texas 12.7 201
## Utah 3.2 120
## Vermont 2.2 48
## Virginia 8.5 156
## Washington 4.0 145
## West Virginia 5.7 81
## Wisconsin 2.6 53
## Wyoming 6.8 161
USArrests[,c(1,3)]
## Murder UrbanPop
## Alabama 13.2 58
## Alaska 10.0 48
## Arizona 8.1 80
## Arkansas 8.8 50
## California 9.0 91
## Colorado 7.9 78
## Connecticut 3.3 77
## Delaware 5.9 72
## Florida 15.4 80
## Georgia 17.4 60
## Hawaii 5.3 83
## Idaho 2.6 54
## Illinois 10.4 83
## Indiana 7.2 65
## Iowa 2.2 57
## Kansas 6.0 66
## Kentucky 9.7 52
## Louisiana 15.4 66
## Maine 2.1 51
## Maryland 11.3 67
## Massachusetts 4.4 85
## Michigan 12.1 74
## Minnesota 2.7 66
## Mississippi 16.1 44
## Missouri 9.0 70
## Montana 6.0 53
## Nebraska 4.3 62
## Nevada 12.2 81
## New Hampshire 2.1 56
## New Jersey 7.4 89
## New Mexico 11.4 70
## New York 11.1 86
## North Carolina 13.0 45
## North Dakota 0.8 44
## Ohio 7.3 75
## Oklahoma 6.6 68
## Oregon 4.9 67
## Pennsylvania 6.3 72
## Rhode Island 3.4 87
## South Carolina 14.4 48
## South Dakota 3.8 45
## Tennessee 13.2 59
## Texas 12.7 80
## Utah 3.2 80
## Vermont 2.2 32
## Virginia 8.5 63
## Washington 4.0 73
## West Virginia 5.7 39
## Wisconsin 2.6 66
## Wyoming 6.8 60
USArrests[1:5,1:2]
## Murder Assault
## Alabama 13.2 236
## Alaska 10.0 263
## Arizona 8.1 294
## Arkansas 8.8 190
## California 9.0 276
USArrests$Murder
## [1] 13.2 10.0 8.1 8.8 9.0 7.9 3.3 5.9 15.4 17.4 5.3 2.6 10.4 7.2
## [15] 2.2 6.0 9.7 15.4 2.1 11.3 4.4 12.1 2.7 16.1 9.0 6.0 4.3 12.2
## [29] 2.1 7.4 11.4 11.1 13.0 0.8 7.3 6.6 4.9 6.3 3.4 14.4 3.8 13.2
## [43] 12.7 3.2 2.2 8.5 4.0 5.7 2.6 6.8
minorMurder = USArrests[order(USArrests$Murder, decreasing = TRUE),]
minorMurder[nrow(minorMurder),]
## Murder Assault UrbanPop Rape
## North Dakota 0.8 45 44 7.3
*¿Que estados tienen una tasa inferior al 4%?, obtén esa información
minorMurder[minorMurder$Murder<4,]
## Murder Assault UrbanPop Rape
## South Dakota 3.8 86 45 12.8
## Rhode Island 3.4 174 87 8.3
## Connecticut 3.3 110 77 11.1
## Utah 3.2 120 80 22.9
## Minnesota 2.7 72 66 14.9
## Idaho 2.6 120 54 14.2
## Wisconsin 2.6 53 66 10.8
## Iowa 2.2 56 57 11.3
## Vermont 2.2 48 32 11.2
## Maine 2.1 83 51 7.8
## New Hampshire 2.1 57 56 9.5
## North Dakota 0.8 45 44 7.3
rownames(USArrests[USArrests[,"UrbanPop"]>quantile(USArrests[,"UrbanPop"],.75),])
## [1] "Arizona" "California" "Colorado" "Florida"
## [5] "Hawaii" "Illinois" "Massachusetts" "Nevada"
## [9] "New Jersey" "New York" "Rhode Island" "Texas"
## [13] "Utah"
Carga el set de datos CO2 y realiza las siguientes acciones:
CO2
## Plant Type Treatment conc uptake
## 1 Qn1 Quebec nonchilled 95 16.0
## 2 Qn1 Quebec nonchilled 175 30.4
## 3 Qn1 Quebec nonchilled 250 34.8
## 4 Qn1 Quebec nonchilled 350 37.2
## 5 Qn1 Quebec nonchilled 500 35.3
## 6 Qn1 Quebec nonchilled 675 39.2
## 7 Qn1 Quebec nonchilled 1000 39.7
## 8 Qn2 Quebec nonchilled 95 13.6
## 9 Qn2 Quebec nonchilled 175 27.3
## 10 Qn2 Quebec nonchilled 250 37.1
## 11 Qn2 Quebec nonchilled 350 41.8
## 12 Qn2 Quebec nonchilled 500 40.6
## 13 Qn2 Quebec nonchilled 675 41.4
## 14 Qn2 Quebec nonchilled 1000 44.3
## 15 Qn3 Quebec nonchilled 95 16.2
## 16 Qn3 Quebec nonchilled 175 32.4
## 17 Qn3 Quebec nonchilled 250 40.3
## 18 Qn3 Quebec nonchilled 350 42.1
## 19 Qn3 Quebec nonchilled 500 42.9
## 20 Qn3 Quebec nonchilled 675 43.9
## 21 Qn3 Quebec nonchilled 1000 45.5
## 22 Qc1 Quebec chilled 95 14.2
## 23 Qc1 Quebec chilled 175 24.1
## 24 Qc1 Quebec chilled 250 30.3
## 25 Qc1 Quebec chilled 350 34.6
## 26 Qc1 Quebec chilled 500 32.5
## 27 Qc1 Quebec chilled 675 35.4
## 28 Qc1 Quebec chilled 1000 38.7
## 29 Qc2 Quebec chilled 95 9.3
## 30 Qc2 Quebec chilled 175 27.3
## 31 Qc2 Quebec chilled 250 35.0
## 32 Qc2 Quebec chilled 350 38.8
## 33 Qc2 Quebec chilled 500 38.6
## 34 Qc2 Quebec chilled 675 37.5
## 35 Qc2 Quebec chilled 1000 42.4
## 36 Qc3 Quebec chilled 95 15.1
## 37 Qc3 Quebec chilled 175 21.0
## 38 Qc3 Quebec chilled 250 38.1
## 39 Qc3 Quebec chilled 350 34.0
## 40 Qc3 Quebec chilled 500 38.9
## 41 Qc3 Quebec chilled 675 39.6
## 42 Qc3 Quebec chilled 1000 41.4
## 43 Mn1 Mississippi nonchilled 95 10.6
## 44 Mn1 Mississippi nonchilled 175 19.2
## 45 Mn1 Mississippi nonchilled 250 26.2
## 46 Mn1 Mississippi nonchilled 350 30.0
## 47 Mn1 Mississippi nonchilled 500 30.9
## 48 Mn1 Mississippi nonchilled 675 32.4
## 49 Mn1 Mississippi nonchilled 1000 35.5
## 50 Mn2 Mississippi nonchilled 95 12.0
## 51 Mn2 Mississippi nonchilled 175 22.0
## 52 Mn2 Mississippi nonchilled 250 30.6
## 53 Mn2 Mississippi nonchilled 350 31.8
## 54 Mn2 Mississippi nonchilled 500 32.4
## 55 Mn2 Mississippi nonchilled 675 31.1
## 56 Mn2 Mississippi nonchilled 1000 31.5
## 57 Mn3 Mississippi nonchilled 95 11.3
## 58 Mn3 Mississippi nonchilled 175 19.4
## 59 Mn3 Mississippi nonchilled 250 25.8
## 60 Mn3 Mississippi nonchilled 350 27.9
## 61 Mn3 Mississippi nonchilled 500 28.5
## 62 Mn3 Mississippi nonchilled 675 28.1
## 63 Mn3 Mississippi nonchilled 1000 27.8
## 64 Mc1 Mississippi chilled 95 10.5
## 65 Mc1 Mississippi chilled 175 14.9
## 66 Mc1 Mississippi chilled 250 18.1
## 67 Mc1 Mississippi chilled 350 18.9
## 68 Mc1 Mississippi chilled 500 19.5
## 69 Mc1 Mississippi chilled 675 22.2
## 70 Mc1 Mississippi chilled 1000 21.9
## 71 Mc2 Mississippi chilled 95 7.7
## 72 Mc2 Mississippi chilled 175 11.4
## 73 Mc2 Mississippi chilled 250 12.3
## 74 Mc2 Mississippi chilled 350 13.0
## 75 Mc2 Mississippi chilled 500 12.5
## 76 Mc2 Mississippi chilled 675 13.7
## 77 Mc2 Mississippi chilled 1000 14.4
## 78 Mc3 Mississippi chilled 95 10.6
## 79 Mc3 Mississippi chilled 175 18.0
## 80 Mc3 Mississippi chilled 250 17.9
## 81 Mc3 Mississippi chilled 350 17.9
## 82 Mc3 Mississippi chilled 500 17.9
## 83 Mc3 Mississippi chilled 675 18.9
## 84 Mc3 Mississippi chilled 1000 19.9
CO2$Plant #is unsorted
## [1] Qn1 Qn1 Qn1 Qn1 Qn1 Qn1 Qn1 Qn2 Qn2 Qn2 Qn2 Qn2 Qn2 Qn2 Qn3 Qn3 Qn3
## [18] Qn3 Qn3 Qn3 Qn3 Qc1 Qc1 Qc1 Qc1 Qc1 Qc1 Qc1 Qc2 Qc2 Qc2 Qc2 Qc2 Qc2
## [35] Qc2 Qc3 Qc3 Qc3 Qc3 Qc3 Qc3 Qc3 Mn1 Mn1 Mn1 Mn1 Mn1 Mn1 Mn1 Mn2 Mn2
## [52] Mn2 Mn2 Mn2 Mn2 Mn2 Mn3 Mn3 Mn3 Mn3 Mn3 Mn3 Mn3 Mc1 Mc1 Mc1 Mc1 Mc1
## [69] Mc1 Mc1 Mc2 Mc2 Mc2 Mc2 Mc2 Mc2 Mc2 Mc3 Mc3 Mc3 Mc3 Mc3 Mc3 Mc3
## 12 Levels: Qn1 < Qn2 < Qn3 < Qc1 < Qc3 < Qc2 < Mn3 < Mn2 < Mn1 < ... < Mc1
co_2ordered <- CO2[order(CO2$Plant),]
co_2ordered
## Plant Type Treatment conc uptake
## 1 Qn1 Quebec nonchilled 95 16.0
## 2 Qn1 Quebec nonchilled 175 30.4
## 3 Qn1 Quebec nonchilled 250 34.8
## 4 Qn1 Quebec nonchilled 350 37.2
## 5 Qn1 Quebec nonchilled 500 35.3
## 6 Qn1 Quebec nonchilled 675 39.2
## 7 Qn1 Quebec nonchilled 1000 39.7
## 8 Qn2 Quebec nonchilled 95 13.6
## 9 Qn2 Quebec nonchilled 175 27.3
## 10 Qn2 Quebec nonchilled 250 37.1
## 11 Qn2 Quebec nonchilled 350 41.8
## 12 Qn2 Quebec nonchilled 500 40.6
## 13 Qn2 Quebec nonchilled 675 41.4
## 14 Qn2 Quebec nonchilled 1000 44.3
## 15 Qn3 Quebec nonchilled 95 16.2
## 16 Qn3 Quebec nonchilled 175 32.4
## 17 Qn3 Quebec nonchilled 250 40.3
## 18 Qn3 Quebec nonchilled 350 42.1
## 19 Qn3 Quebec nonchilled 500 42.9
## 20 Qn3 Quebec nonchilled 675 43.9
## 21 Qn3 Quebec nonchilled 1000 45.5
## 22 Qc1 Quebec chilled 95 14.2
## 23 Qc1 Quebec chilled 175 24.1
## 24 Qc1 Quebec chilled 250 30.3
## 25 Qc1 Quebec chilled 350 34.6
## 26 Qc1 Quebec chilled 500 32.5
## 27 Qc1 Quebec chilled 675 35.4
## 28 Qc1 Quebec chilled 1000 38.7
## 36 Qc3 Quebec chilled 95 15.1
## 37 Qc3 Quebec chilled 175 21.0
## 38 Qc3 Quebec chilled 250 38.1
## 39 Qc3 Quebec chilled 350 34.0
## 40 Qc3 Quebec chilled 500 38.9
## 41 Qc3 Quebec chilled 675 39.6
## 42 Qc3 Quebec chilled 1000 41.4
## 29 Qc2 Quebec chilled 95 9.3
## 30 Qc2 Quebec chilled 175 27.3
## 31 Qc2 Quebec chilled 250 35.0
## 32 Qc2 Quebec chilled 350 38.8
## 33 Qc2 Quebec chilled 500 38.6
## 34 Qc2 Quebec chilled 675 37.5
## 35 Qc2 Quebec chilled 1000 42.4
## 57 Mn3 Mississippi nonchilled 95 11.3
## 58 Mn3 Mississippi nonchilled 175 19.4
## 59 Mn3 Mississippi nonchilled 250 25.8
## 60 Mn3 Mississippi nonchilled 350 27.9
## 61 Mn3 Mississippi nonchilled 500 28.5
## 62 Mn3 Mississippi nonchilled 675 28.1
## 63 Mn3 Mississippi nonchilled 1000 27.8
## 50 Mn2 Mississippi nonchilled 95 12.0
## 51 Mn2 Mississippi nonchilled 175 22.0
## 52 Mn2 Mississippi nonchilled 250 30.6
## 53 Mn2 Mississippi nonchilled 350 31.8
## 54 Mn2 Mississippi nonchilled 500 32.4
## 55 Mn2 Mississippi nonchilled 675 31.1
## 56 Mn2 Mississippi nonchilled 1000 31.5
## 43 Mn1 Mississippi nonchilled 95 10.6
## 44 Mn1 Mississippi nonchilled 175 19.2
## 45 Mn1 Mississippi nonchilled 250 26.2
## 46 Mn1 Mississippi nonchilled 350 30.0
## 47 Mn1 Mississippi nonchilled 500 30.9
## 48 Mn1 Mississippi nonchilled 675 32.4
## 49 Mn1 Mississippi nonchilled 1000 35.5
## 71 Mc2 Mississippi chilled 95 7.7
## 72 Mc2 Mississippi chilled 175 11.4
## 73 Mc2 Mississippi chilled 250 12.3
## 74 Mc2 Mississippi chilled 350 13.0
## 75 Mc2 Mississippi chilled 500 12.5
## 76 Mc2 Mississippi chilled 675 13.7
## 77 Mc2 Mississippi chilled 1000 14.4
## 78 Mc3 Mississippi chilled 95 10.6
## 79 Mc3 Mississippi chilled 175 18.0
## 80 Mc3 Mississippi chilled 250 17.9
## 81 Mc3 Mississippi chilled 350 17.9
## 82 Mc3 Mississippi chilled 500 17.9
## 83 Mc3 Mississippi chilled 675 18.9
## 84 Mc3 Mississippi chilled 1000 19.9
## 64 Mc1 Mississippi chilled 95 10.5
## 65 Mc1 Mississippi chilled 175 14.9
## 66 Mc1 Mississippi chilled 250 18.1
## 67 Mc1 Mississippi chilled 350 18.9
## 68 Mc1 Mississippi chilled 500 19.5
## 69 Mc1 Mississippi chilled 675 22.2
## 70 Mc1 Mississippi chilled 1000 21.9
is.unsorted(co_2ordered$Plant)
## [1] FALSE
students<-read.table("./datasets/student.txt", header = TRUE)
students
## height shoesize gender population
## 1 181 44 male kuopio
## 2 160 38 female kuopio
## 3 174 42 female kuopio
## 4 170 43 male kuopio
## 5 172 43 male kuopio
## 6 165 39 female kuopio
## 7 161 38 female kuopio
## 8 167 38 female tampere
## 9 164 39 female tampere
## 10 166 38 female tampere
## 11 162 37 female tampere
## 12 158 36 female tampere
## 13 175 42 male tampere
## 14 181 44 male tampere
## 15 180 43 male tampere
## 16 177 43 male tampere
## 17 173 41 male tampere
colnames(students)
## [1] "height" "shoesize" "gender" "population"
students$height
## [1] 181 160 174 170 172 165 161 167 164 166 162 158 175 181 180 177 173
table(students)
## , , gender = female, population = kuopio
##
## shoesize
## height 36 37 38 39 41 42 43 44
## 158 0 0 0 0 0 0 0 0
## 160 0 0 1 0 0 0 0 0
## 161 0 0 1 0 0 0 0 0
## 162 0 0 0 0 0 0 0 0
## 164 0 0 0 0 0 0 0 0
## 165 0 0 0 1 0 0 0 0
## 166 0 0 0 0 0 0 0 0
## 167 0 0 0 0 0 0 0 0
## 170 0 0 0 0 0 0 0 0
## 172 0 0 0 0 0 0 0 0
## 173 0 0 0 0 0 0 0 0
## 174 0 0 0 0 0 1 0 0
## 175 0 0 0 0 0 0 0 0
## 177 0 0 0 0 0 0 0 0
## 180 0 0 0 0 0 0 0 0
## 181 0 0 0 0 0 0 0 0
##
## , , gender = male, population = kuopio
##
## shoesize
## height 36 37 38 39 41 42 43 44
## 158 0 0 0 0 0 0 0 0
## 160 0 0 0 0 0 0 0 0
## 161 0 0 0 0 0 0 0 0
## 162 0 0 0 0 0 0 0 0
## 164 0 0 0 0 0 0 0 0
## 165 0 0 0 0 0 0 0 0
## 166 0 0 0 0 0 0 0 0
## 167 0 0 0 0 0 0 0 0
## 170 0 0 0 0 0 0 1 0
## 172 0 0 0 0 0 0 1 0
## 173 0 0 0 0 0 0 0 0
## 174 0 0 0 0 0 0 0 0
## 175 0 0 0 0 0 0 0 0
## 177 0 0 0 0 0 0 0 0
## 180 0 0 0 0 0 0 0 0
## 181 0 0 0 0 0 0 0 1
##
## , , gender = female, population = tampere
##
## shoesize
## height 36 37 38 39 41 42 43 44
## 158 1 0 0 0 0 0 0 0
## 160 0 0 0 0 0 0 0 0
## 161 0 0 0 0 0 0 0 0
## 162 0 1 0 0 0 0 0 0
## 164 0 0 0 1 0 0 0 0
## 165 0 0 0 0 0 0 0 0
## 166 0 0 1 0 0 0 0 0
## 167 0 0 1 0 0 0 0 0
## 170 0 0 0 0 0 0 0 0
## 172 0 0 0 0 0 0 0 0
## 173 0 0 0 0 0 0 0 0
## 174 0 0 0 0 0 0 0 0
## 175 0 0 0 0 0 0 0 0
## 177 0 0 0 0 0 0 0 0
## 180 0 0 0 0 0 0 0 0
## 181 0 0 0 0 0 0 0 0
##
## , , gender = male, population = tampere
##
## shoesize
## height 36 37 38 39 41 42 43 44
## 158 0 0 0 0 0 0 0 0
## 160 0 0 0 0 0 0 0 0
## 161 0 0 0 0 0 0 0 0
## 162 0 0 0 0 0 0 0 0
## 164 0 0 0 0 0 0 0 0
## 165 0 0 0 0 0 0 0 0
## 166 0 0 0 0 0 0 0 0
## 167 0 0 0 0 0 0 0 0
## 170 0 0 0 0 0 0 0 0
## 172 0 0 0 0 0 0 0 0
## 173 0 0 0 0 1 0 0 0
## 174 0 0 0 0 0 0 0 0
## 175 0 0 0 0 0 1 0 0
## 177 0 0 0 0 0 0 1 0
## 180 0 0 0 0 0 0 1 0
## 181 0 0 0 0 0 0 0 1
sym<-ifelse(students$gender=="male", "M", "F")
colours<-ifelse(students$population=="kuopio", "Blue", "Red")
students.new<-cbind(students,sym, colours)
str(students.new)
## 'data.frame': 17 obs. of 6 variables:
## $ height : int 181 160 174 170 172 165 161 167 164 166 ...
## $ shoesize : int 44 38 42 43 43 39 38 38 39 38 ...
## $ gender : Factor w/ 2 levels "female","male": 2 1 1 2 2 1 1 1 1 1 ...
## $ population: Factor w/ 2 levels "kuopio","tampere": 1 1 1 1 1 1 1 2 2 2 ...
## $ sym : Factor w/ 2 levels "F","M": 2 1 1 2 2 1 1 1 1 1 ...
## $ colours : Factor w/ 2 levels "Blue","Red": 1 1 1 1 1 1 1 2 2 2 ...
students.male = students.new[which(students.new$sym=="M"),]
students.female = students.new[which(students.new$sym=="F"),]
write.table(students.new, "./datasets/studentsnew.txt", col.names = TRUE)
my_list <- list(name="Fred", wife="Mary", nochildren=3, child.ages=c(4,7,9))
*Crea una lista con ciertos atributos y valores
my_list2<- list(empleado="Jose", trabajo="Analista Datos", meses_trabajo=c(month.abb[1:3]))
*Muestra los atributos, (nombre de las variables) de nuestra lista
names(my_list2)
## [1] "empleado" "trabajo" "meses_trabajo"
my_list2[1:3]
## $empleado
## [1] "Jose"
##
## $trabajo
## [1] "Analista Datos"
##
## $meses_trabajo
## [1] "Jan" "Feb" "Mar"
my_list2[2]
## $trabajo
## [1] "Analista Datos"
my_list2[[2]]
## [1] "Analista Datos"
my_list$wife[1]
## [1] "Mary"
my_list$child.ages[3]
## [1] 9
length(my_list[[4]])
## [1] 3
my_list$wife <- 1:12
my_list$wife <- NULL
my_list <- c(my_list, list(my_title2=month.name[1:12]))
data.frame(unlist(my_list))
## unlist.my_list.
## name Fred
## nochildren 3
## child.ages1 4
## child.ages2 7
## child.ages3 9
## my_title21 January
## my_title22 February
## my_title23 March
## my_title24 April
## my_title25 May
## my_title26 June
## my_title27 July
## my_title28 August
## my_title29 September
## my_title210 October
## my_title211 November
## my_title212 December
matrix(unlist(my_list))
## [,1]
## [1,] "Fred"
## [2,] "3"
## [3,] "4"
## [4,] "7"
## [5,] "9"
## [6,] "January"
## [7,] "February"
## [8,] "March"
## [9,] "April"
## [10,] "May"
## [11,] "June"
## [12,] "July"
## [13,] "August"
## [14,] "September"
## [15,] "October"
## [16,] "November"
## [17,] "December"
La función table() cuenta el número de elementos repetidos en un vector. Es la función más básica de clustering.
table(iris$Sepal.Length)
##
## 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6
## 1 3 1 4 2 5 6 10 9 4 1 6 7 6 8 7 3 6
## 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7 7.1 7.2 7.3 7.4 7.6 7.7 7.9
## 6 4 9 7 5 2 8 3 4 1 1 3 1 1 1 4 1
Vamos a volver a utilizar el datasets mtcars.
mtcars[order(mtcars$hp),]
## mpg cyl disp hp drat wt qsec vs am gear carb
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## mpgClass
## Honda Civic High
## Merc 240D High
## Toyota Corolla High
## Fiat 128 High
## Fiat X1-9 High
## Porsche 914-2 High
## Datsun 710 High
## Merc 230 High
## Toyota Corona High
## Valiant Low
## Volvo 142E High
## Mazda RX4 High
## Mazda RX4 Wag High
## Hornet 4 Drive High
## Lotus Europa High
## Merc 280 Low
## Merc 280C Low
## Dodge Challenger Low
## AMC Javelin Low
## Hornet Sportabout Low
## Pontiac Firebird Low
## Ferrari Dino Low
## Merc 450SE Low
## Merc 450SL Low
## Merc 450SLC Low
## Cadillac Fleetwood Low
## Lincoln Continental Low
## Chrysler Imperial Low
## Duster 360 Low
## Camaro Z28 Low
## Ford Pantera L Low
## Maserati Bora Low
mtcars[order(mtcars$hp, decreasing = TRUE),]
## mpg cyl disp hp drat wt qsec vs am gear carb
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## mpgClass
## Maserati Bora Low
## Ford Pantera L Low
## Duster 360 Low
## Camaro Z28 Low
## Chrysler Imperial Low
## Lincoln Continental Low
## Cadillac Fleetwood Low
## Merc 450SE Low
## Merc 450SL Low
## Merc 450SLC Low
## Hornet Sportabout Low
## Pontiac Firebird Low
## Ferrari Dino Low
## Dodge Challenger Low
## AMC Javelin Low
## Merc 280 Low
## Merc 280C Low
## Lotus Europa High
## Mazda RX4 High
## Mazda RX4 Wag High
## Hornet 4 Drive High
## Volvo 142E High
## Valiant Low
## Toyota Corona High
## Merc 230 High
## Datsun 710 High
## Porsche 914-2 High
## Fiat 128 High
## Fiat X1-9 High
## Toyota Corolla High
## Merc 240D High
## Honda Civic High
mean(mtcars$mpg)
## [1] 20.09062
Calcula la media de mpg para aquellos datos cuyo valor de hp sea menor que 150 y por separado para aquellos cuyo valor de hp sea mayor o igual a 150
mean(mtcars[mtcars$hp<150,]$mpg)
## [1] 24.22353
mean(mtcars[mtcars$hp>150,]$mpg)
## [1] 15.41538
unique(mtcars$cyl)
## [1] 6 4 8
mtcars["Toyota Corolla",c("mpg","cyl","disp","hp")]
## mpg cyl disp hp
## Toyota Corolla 33.9 4 71.1 65
mtcars$mpgClass<-ifelse(mtcars$mpg<(mean(mtcars$mpg)),"Low", "High")