0% found this document useful (0 votes)
43 views4 pages

File Show

This document loads car data from a CSV file into a variable called "datos". It then calculates summary statistics like the mean, median, and mode for the mpg variable. It also creates a data frame combining mpg and hp variables, and calculates their covariance, correlation, and the correlation matrix. Finally, it finds the standard deviation of the mpg variable.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views4 pages

File Show

This document loads car data from a CSV file into a variable called "datos". It then calculates summary statistics like the mean, median, and mode for the mpg variable. It also creates a data frame combining mpg and hp variables, and calculates their covariance, correlation, and the correlation matrix. Finally, it finds the standard deviation of the mpg variable.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

R Notebook

Almacenamos la informació n de “mtcars.csv” en la variable “datos”.


datos <- read.csv("mtcars.csv")
print(datos)

## mpg cyl disp hp drat wt qsec vs am gear carb fast


## 1 4.582576 6 160.0 110 3.90 2.620 16.46 0 1 4 4 1
## 2 4.582576 6 160.0 110 3.90 2.875 17.02 0 1 4 4 1
## 3 4.774935 4 108.0 93 3.85 2.320 18.61 1 1 4 1 1
## 4 4.626013 6 258.0 110 3.08 3.215 19.44 1 0 3 1 1
## 5 4.324350 8 360.0 175 3.15 3.440 17.02 0 0 3 2 1
## 6 4.254409 6 225.0 105 2.76 3.460 20.22 1 0 3 1 1
## 7 3.781534 8 360.0 245 3.21 3.570 15.84 0 0 3 4 0
## 8 4.939636 4 146.7 62 3.69 3.190 20.00 1 0 4 2 1
## 9 4.774935 4 140.8 95 3.92 3.150 22.90 1 0 4 2 1
## 10 4.381780 6 167.6 123 3.92 3.440 18.30 1 0 4 4 1
## 11 4.219005 6 167.6 123 3.92 3.440 18.90 1 0 4 4 1
## 12 4.049691 8 275.8 180 3.07 4.070 17.40 0 0 3 3 1
## 13 4.159327 8 275.8 180 3.07 3.730 17.60 0 0 3 3 1
## 14 3.898718 8 275.8 180 3.07 3.780 18.00 0 0 3 3 0
## 15 3.224903 8 472.0 205 2.93 5.250 17.98 0 0 3 4 0
## 16 3.224903 8 460.0 215 3.00 5.424 17.82 0 0 3 4 0
## 17 3.834058 8 440.0 230 3.23 5.345 17.42 0 0 3 4 0
## 18 5.692100 4 78.7 66 4.08 2.200 19.47 1 1 4 1 1
## 19 5.513620 4 75.7 52 4.93 1.615 18.52 1 1 4 2 1
## 20 5.822371 4 71.1 65 4.22 1.835 19.90 1 1 4 1 1
## 21 4.636809 4 120.1 97 3.70 2.465 20.01 1 0 3 1 1
## 22 3.937004 8 318.0 150 2.76 3.520 16.87 0 0 3 2 0
## 23 3.898718 8 304.0 150 3.15 3.435 17.30 0 0 3 2 0
## 24 3.646917 8 350.0 245 3.73 3.840 15.41 0 0 3 4 0
## 25 4.381780 8 400.0 175 3.08 3.845 17.05 0 0 3 2 1
## 26 5.224940 4 79.0 66 4.08 1.935 18.90 1 1 4 1 1
## 27 5.099020 4 120.3 91 4.43 2.140 16.70 0 1 5 2 1
## 28 5.513620 4 95.1 113 3.77 1.513 16.90 1 1 5 2 1
## 29 3.974921 8 351.0 264 4.22 3.170 14.50 0 1 5 4 0
## 30 4.438468 6 145.0 175 3.62 2.770 15.50 0 1 5 6 1
## 31 3.872983 8 301.0 335 3.54 3.570 14.60 0 1 5 8 0
## 32 4.626013 4 121.0 109 4.11 2.780 18.60 1 1 4 2 1
## cars carname
## 1 Mazda RX4 Mazda RX4
## 2 Mazda RX4 Wag Mazda RX4 Wag
## 3 Datsun 710 Datsun 710
## 4 Hornet 4 Drive Hornet 4 Drive
## 5 Hornet Sportabout Hornet Sportabout
## 6 Valiant Valiant
## 7 Duster 360 Duster 360
## 8 Merc 240D Merc 240D
## 9 Merc 230 Merc 230
## 10 Merc 280 Merc 280
## 11 Merc 280C Merc 280C
## 12 Merc 450SE Merc 450SE
## 13 Merc 450SL Merc 450SL
## 14 Merc 450SLC Merc 450SLC
## 15 Cadillac Fleetwood Cadillac Fleetwood
## 16 Lincoln Continental Lincoln Continental
## 17 Chrysler Imperial Chrysler Imperial
## 18 Fiat 128 Fiat 128
## 19 Honda Civic Honda Civic
## 20 Toyota Corolla Toyota Corolla
## 21 Toyota Corona Toyota Corona
## 22 Dodge Challenger Dodge Challenger
## 23 AMC Javelin AMC Javelin
## 24 Camaro Z28 Camaro Z28
## 25 Pontiac Firebird Pontiac Firebird
## 26 Fiat X1-9 Fiat X1-9
## 27 Porsche 914-2 Porsche 914-2
## 28 Lotus Europa Lotus Europa
## 29 Ford Pantera L Ford Pantera L
## 30 Ferrari Dino Ferrari Dino
## 31 Maserati Bora Maserati Bora
## 32 Volvo 142E Volvo 142E

Calculamos la media de mpg.


media <- mean(datos$mpg)
print(media)

## [1] 4.43477

Calculamos la mediana de mpg.


mediana <- median(datos$mpg)
print(mediana)

## [1] 4.38178

Creamos la funció n para calcular la moda.


mode <- function(x){ ux <- unique(x)
+ ux[which.max(tabulate(match(x, ux)))]}

Almacenamos la informació n de mpg en la variable datos_mpg.


datos_mpg <- datos$mpg
print(datos_mpg)

## [1] 4.582576 4.582576 4.774935 4.626013 4.324350 4.254409 3.781534


4.939636
## [9] 4.774935 4.381780 4.219005 4.049691 4.159327 3.898718 3.224903
3.224903
## [17] 3.834058 5.692100 5.513620 5.822371 4.636809 3.937004 3.898718
3.646917
## [25] 4.381780 5.224940 5.099020 5.513620 3.974921 4.438468 3.872983
4.626013

Calculamos la moda de los datos almacenados en datos_mpg.


moda <- mode(datos_mpg)
print(moda)

## [1] 4.582576

Almacenamos los datos de hp en datos_hp.


datos_hp <- datos$hp
print(datos_hp)

## [1] 110 110 93 110 175 105 245 62 95 123 123 180 180 180 205 215 230
66 52
## [20] 65 97 150 150 245 175 66 91 113 264 175 335 109

Creamos el data.frame respecto a datos_mpg y datos_hp.


datos_mpg_hp <- data.frame(datos_mpg, datos_hp)
print(datos_mpg_hp)

## datos_mpg datos_hp
## 1 4.582576 110
## 2 4.582576 110
## 3 4.774935 93
## 4 4.626013 110
## 5 4.324350 175
## 6 4.254409 105
## 7 3.781534 245
## 8 4.939636 62
## 9 4.774935 95
## 10 4.381780 123
## 11 4.219005 123
## 12 4.049691 180
## 13 4.159327 180
## 14 3.898718 180
## 15 3.224903 205
## 16 3.224903 215
## 17 3.834058 230
## 18 5.692100 66
## 19 5.513620 52
## 20 5.822371 65
## 21 4.636809 97
## 22 3.937004 150
## 23 3.898718 150
## 24 3.646917 245
## 25 4.381780 175
## 26 5.224940 66
## 27 5.099020 91
## 28 5.513620 113
## 29 3.974921 264
## 30 4.438468 175
## 31 3.872983 335
## 32 4.626013 109

Covarianza de datos_mpg y datos_hp.


covarianza_mpg_hp <- cov(datos_mpg, datos_hp, use = "everything")
print(covarianza_mpg_hp)

## [1] -35.68827

Correlació n de datos entre mpg y hp.


correlaciones <- cor(datos_mpg, datos_hp)
print(correlaciones)

## [1] -0.787309

Matriz de correlaciones datos_mpg_hp.


matriz_corr <- cor(datos_mpg_hp)
print(matriz_corr)

## datos_mpg datos_hp
## datos_mpg 1.000000 -0.787309
## datos_hp -0.787309 1.000000

Calculamos desviació n estandar de mpg.


desviación_estandar_mpg <- sd(datos_mpg)
print(desviación_estandar_mpg)

## [1] 0.6611368

You might also like