{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# How to manipulate dataframe in R\n",
"\n",
"Here we give a little tutorial to illustrate how to use dataframe in R. We will use the R-built in dataframe ```mtcars```\n",
"\n",
"we will first make a copy of the dataset as we are going to make some modification later"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"df = mtcars"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Take a look of the dataframe\n",
"\n",
"The simpliest way to have a look of the first few rows of the dataset is to use the function [```head```](https://stat.ethz.ch/R-manual/R-devel/library/utils/html/head.html)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
" | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
\n",
"\n",
"\tMazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
\n",
"\tMazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
\n",
"\tDatsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
\n",
"\tHornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
\n",
"\tHornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
\n",
"\tValiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lllllllllll}\n",
" & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\\\\n",
"\\hline\n",
"\tMazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4 \\\\\n",
"\tMazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\\\\n",
"\tDatsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1 \\\\\n",
"\tHornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1 \\\\\n",
"\tHornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2 \\\\\n",
"\tValiant & 18.1 & 6 & 225 & 105 & 2.76 & 3.460 & 20.22 & 1 & 0 & 3 & 1 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | \n",
"|---|---|---|---|---|---|\n",
"| Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 | \n",
"| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 | \n",
"| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 | \n",
"| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 | \n",
"| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 | \n",
"| Valiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 | \n",
"\n",
"\n"
],
"text/plain": [
" mpg cyl disp hp drat wt qsec vs am gear carb\n",
"Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 \n",
"Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 \n",
"Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 \n",
"Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 \n",
"Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 \n",
"Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"head(df) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can look at the last few rows by the function [```tail```](https://stat.ethz.ch/R-manual/R-devel/library/utils/html/head.html)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
\n",
"\n",
"\tPorsche 914-2 | 26.0 | 4 | 120.3 | 91 | 4.43 | 2.140 | 16.7 | 0 | 1 | 5 | 2 |
\n",
"\tLotus Europa | 30.4 | 4 | 95.1 | 113 | 3.77 | 1.513 | 16.9 | 1 | 1 | 5 | 2 |
\n",
"\tFord Pantera L | 15.8 | 8 | 351.0 | 264 | 4.22 | 3.170 | 14.5 | 0 | 1 | 5 | 4 |
\n",
"\tFerrari Dino | 19.7 | 6 | 145.0 | 175 | 3.62 | 2.770 | 15.5 | 0 | 1 | 5 | 6 |
\n",
"\tMaserati Bora | 15.0 | 8 | 301.0 | 335 | 3.54 | 3.570 | 14.6 | 0 | 1 | 5 | 8 |
\n",
"\tVolvo 142E | 21.4 | 4 | 121.0 | 109 | 4.11 | 2.780 | 18.6 | 1 | 1 | 4 | 2 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lllllllllll}\n",
" & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\\\\n",
"\\hline\n",
"\tPorsche 914-2 & 26.0 & 4 & 120.3 & 91 & 4.43 & 2.140 & 16.7 & 0 & 1 & 5 & 2 \\\\\n",
"\tLotus Europa & 30.4 & 4 & 95.1 & 113 & 3.77 & 1.513 & 16.9 & 1 & 1 & 5 & 2 \\\\\n",
"\tFord Pantera L & 15.8 & 8 & 351.0 & 264 & 4.22 & 3.170 & 14.5 & 0 & 1 & 5 & 4 \\\\\n",
"\tFerrari Dino & 19.7 & 6 & 145.0 & 175 & 3.62 & 2.770 & 15.5 & 0 & 1 & 5 & 6 \\\\\n",
"\tMaserati Bora & 15.0 & 8 & 301.0 & 335 & 3.54 & 3.570 & 14.6 & 0 & 1 & 5 & 8 \\\\\n",
"\tVolvo 142E & 21.4 & 4 & 121.0 & 109 & 4.11 & 2.780 & 18.6 & 1 & 1 & 4 & 2 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | \n",
"|---|---|---|---|---|---|\n",
"| Porsche 914-2 | 26.0 | 4 | 120.3 | 91 | 4.43 | 2.140 | 16.7 | 0 | 1 | 5 | 2 | \n",
"| Lotus Europa | 30.4 | 4 | 95.1 | 113 | 3.77 | 1.513 | 16.9 | 1 | 1 | 5 | 2 | \n",
"| Ford Pantera L | 15.8 | 8 | 351.0 | 264 | 4.22 | 3.170 | 14.5 | 0 | 1 | 5 | 4 | \n",
"| Ferrari Dino | 19.7 | 6 | 145.0 | 175 | 3.62 | 2.770 | 15.5 | 0 | 1 | 5 | 6 | \n",
"| Maserati Bora | 15.0 | 8 | 301.0 | 335 | 3.54 | 3.570 | 14.6 | 0 | 1 | 5 | 8 | \n",
"| Volvo 142E | 21.4 | 4 | 121.0 | 109 | 4.11 | 2.780 | 18.6 | 1 | 1 | 4 | 2 | \n",
"\n",
"\n"
],
"text/plain": [
" mpg cyl disp hp drat wt qsec vs am gear carb\n",
"Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.7 0 1 5 2 \n",
"Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2 \n",
"Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.5 0 1 5 4 \n",
"Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.5 0 1 5 6 \n",
"Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.6 0 1 5 8 \n",
"Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.6 1 1 4 2 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"tail(df)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Get and set rownames / colnames\n",
"The left most shows the rownames of the df and the top shows the colnames. We can retreive the rownames simply by using the function [```rownames```](https://stat.ethz.ch/R-manual/R-devel/library/base/html/colnames.html)!"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- 'Mazda RX4'
\n",
"\t- 'Mazda RX4 Wag'
\n",
"\t- 'Datsun 710'
\n",
"\t- 'Hornet 4 Drive'
\n",
"\t- 'Hornet Sportabout'
\n",
"\t- 'Valiant'
\n",
"\t- 'Duster 360'
\n",
"\t- 'Merc 240D'
\n",
"\t- 'Merc 230'
\n",
"\t- 'Merc 280'
\n",
"\t- 'Merc 280C'
\n",
"\t- 'Merc 450SE'
\n",
"\t- 'Merc 450SL'
\n",
"\t- 'Merc 450SLC'
\n",
"\t- 'Cadillac Fleetwood'
\n",
"\t- 'Lincoln Continental'
\n",
"\t- 'Chrysler Imperial'
\n",
"\t- 'Fiat 128'
\n",
"\t- 'Honda Civic'
\n",
"\t- 'Toyota Corolla'
\n",
"\t- 'Toyota Corona'
\n",
"\t- 'Dodge Challenger'
\n",
"\t- 'AMC Javelin'
\n",
"\t- 'Camaro Z28'
\n",
"\t- 'Pontiac Firebird'
\n",
"\t- 'Fiat X1-9'
\n",
"\t- 'Porsche 914-2'
\n",
"\t- 'Lotus Europa'
\n",
"\t- 'Ford Pantera L'
\n",
"\t- 'Ferrari Dino'
\n",
"\t- 'Maserati Bora'
\n",
"\t- 'Volvo 142E'
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 'Mazda RX4'\n",
"\\item 'Mazda RX4 Wag'\n",
"\\item 'Datsun 710'\n",
"\\item 'Hornet 4 Drive'\n",
"\\item 'Hornet Sportabout'\n",
"\\item 'Valiant'\n",
"\\item 'Duster 360'\n",
"\\item 'Merc 240D'\n",
"\\item 'Merc 230'\n",
"\\item 'Merc 280'\n",
"\\item 'Merc 280C'\n",
"\\item 'Merc 450SE'\n",
"\\item 'Merc 450SL'\n",
"\\item 'Merc 450SLC'\n",
"\\item 'Cadillac Fleetwood'\n",
"\\item 'Lincoln Continental'\n",
"\\item 'Chrysler Imperial'\n",
"\\item 'Fiat 128'\n",
"\\item 'Honda Civic'\n",
"\\item 'Toyota Corolla'\n",
"\\item 'Toyota Corona'\n",
"\\item 'Dodge Challenger'\n",
"\\item 'AMC Javelin'\n",
"\\item 'Camaro Z28'\n",
"\\item 'Pontiac Firebird'\n",
"\\item 'Fiat X1-9'\n",
"\\item 'Porsche 914-2'\n",
"\\item 'Lotus Europa'\n",
"\\item 'Ford Pantera L'\n",
"\\item 'Ferrari Dino'\n",
"\\item 'Maserati Bora'\n",
"\\item 'Volvo 142E'\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 'Mazda RX4'\n",
"2. 'Mazda RX4 Wag'\n",
"3. 'Datsun 710'\n",
"4. 'Hornet 4 Drive'\n",
"5. 'Hornet Sportabout'\n",
"6. 'Valiant'\n",
"7. 'Duster 360'\n",
"8. 'Merc 240D'\n",
"9. 'Merc 230'\n",
"10. 'Merc 280'\n",
"11. 'Merc 280C'\n",
"12. 'Merc 450SE'\n",
"13. 'Merc 450SL'\n",
"14. 'Merc 450SLC'\n",
"15. 'Cadillac Fleetwood'\n",
"16. 'Lincoln Continental'\n",
"17. 'Chrysler Imperial'\n",
"18. 'Fiat 128'\n",
"19. 'Honda Civic'\n",
"20. 'Toyota Corolla'\n",
"21. 'Toyota Corona'\n",
"22. 'Dodge Challenger'\n",
"23. 'AMC Javelin'\n",
"24. 'Camaro Z28'\n",
"25. 'Pontiac Firebird'\n",
"26. 'Fiat X1-9'\n",
"27. 'Porsche 914-2'\n",
"28. 'Lotus Europa'\n",
"29. 'Ford Pantera L'\n",
"30. 'Ferrari Dino'\n",
"31. 'Maserati Bora'\n",
"32. 'Volvo 142E'\n",
"\n",
"\n"
],
"text/plain": [
" [1] \"Mazda RX4\" \"Mazda RX4 Wag\" \"Datsun 710\" \n",
" [4] \"Hornet 4 Drive\" \"Hornet Sportabout\" \"Valiant\" \n",
" [7] \"Duster 360\" \"Merc 240D\" \"Merc 230\" \n",
"[10] \"Merc 280\" \"Merc 280C\" \"Merc 450SE\" \n",
"[13] \"Merc 450SL\" \"Merc 450SLC\" \"Cadillac Fleetwood\" \n",
"[16] \"Lincoln Continental\" \"Chrysler Imperial\" \"Fiat 128\" \n",
"[19] \"Honda Civic\" \"Toyota Corolla\" \"Toyota Corona\" \n",
"[22] \"Dodge Challenger\" \"AMC Javelin\" \"Camaro Z28\" \n",
"[25] \"Pontiac Firebird\" \"Fiat X1-9\" \"Porsche 914-2\" \n",
"[28] \"Lotus Europa\" \"Ford Pantera L\" \"Ferrari Dino\" \n",
"[31] \"Maserati Bora\" \"Volvo 142E\" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"rownames(df)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If for example we want to set the first rownames from \"Mazda RX4\" to \"Mazda RX5\", we can set the rownames simply by:"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
\n",
"\n",
"\tMazda RX5 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
\n",
"\tMazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
\n",
"\tDatsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
\n",
"\tHornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
\n",
"\tHornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
\n",
"\tValiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lllllllllll}\n",
" & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\\\\n",
"\\hline\n",
"\tMazda RX5 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4 \\\\\n",
"\tMazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\\\\n",
"\tDatsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1 \\\\\n",
"\tHornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1 \\\\\n",
"\tHornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2 \\\\\n",
"\tValiant & 18.1 & 6 & 225 & 105 & 2.76 & 3.460 & 20.22 & 1 & 0 & 3 & 1 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | \n",
"|---|---|---|---|---|---|\n",
"| Mazda RX5 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 | \n",
"| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 | \n",
"| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 | \n",
"| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 | \n",
"| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 | \n",
"| Valiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 | \n",
"\n",
"\n"
],
"text/plain": [
" mpg cyl disp hp drat wt qsec vs am gear carb\n",
"Mazda RX5 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 \n",
"Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 \n",
"Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 \n",
"Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 \n",
"Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 \n",
"Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"rownames(df)[1] = \"Mazda RX5\"\n",
"head(df)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Remember R indexes starts from 1!\n",
"\n",
"Similarly we can get the colnames and change them by using the function [```colnames```](https://stat.ethz.ch/R-manual/R-devel/library/base/html/colnames.html)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" [1] \"mpg\" \"cyl\" \"disp\" \"hp\" \"drat\" \"wt\" \"qsec\" \"vs\" \"am\" \"gear\"\n",
"[11] \"carb\"\n"
]
},
{
"data": {
"text/html": [
"\n",
" | a | b | c | d | e | f | g | h | i | j | k |
\n",
"\n",
"\tMazda RX5 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
\n",
"\tMazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
\n",
"\tDatsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
\n",
"\tHornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
\n",
"\tHornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
\n",
"\tValiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lllllllllll}\n",
" & a & b & c & d & e & f & g & h & i & j & k\\\\\n",
"\\hline\n",
"\tMazda RX5 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4 \\\\\n",
"\tMazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\\\\n",
"\tDatsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1 \\\\\n",
"\tHornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1 \\\\\n",
"\tHornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2 \\\\\n",
"\tValiant & 18.1 & 6 & 225 & 105 & 2.76 & 3.460 & 20.22 & 1 & 0 & 3 & 1 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | a | b | c | d | e | f | g | h | i | j | k | \n",
"|---|---|---|---|---|---|\n",
"| Mazda RX5 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 | \n",
"| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 | \n",
"| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 | \n",
"| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 | \n",
"| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 | \n",
"| Valiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 | \n",
"\n",
"\n"
],
"text/plain": [
" a b c d e f g h i j k\n",
"Mazda RX5 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4\n",
"Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4\n",
"Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1\n",
"Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1\n",
"Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2\n",
"Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"print(colnames(df))\n",
"colnames(df) = c(\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\", \"k\") # c(...) is a vector of different objects \n",
"head(df)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's change the colnames to the original colnames before we continue..."
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
\n",
"\n",
"\tMazda RX5 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
\n",
"\tMazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
\n",
"\tDatsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
\n",
"\tHornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
\n",
"\tHornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
\n",
"\tValiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lllllllllll}\n",
" & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\\\\n",
"\\hline\n",
"\tMazda RX5 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4 \\\\\n",
"\tMazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\\\\n",
"\tDatsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1 \\\\\n",
"\tHornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1 \\\\\n",
"\tHornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2 \\\\\n",
"\tValiant & 18.1 & 6 & 225 & 105 & 2.76 & 3.460 & 20.22 & 1 & 0 & 3 & 1 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | \n",
"|---|---|---|---|---|---|\n",
"| Mazda RX5 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 | \n",
"| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 | \n",
"| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 | \n",
"| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 | \n",
"| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 | \n",
"| Valiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 | \n",
"\n",
"\n"
],
"text/plain": [
" mpg cyl disp hp drat wt qsec vs am gear carb\n",
"Mazda RX5 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 \n",
"Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 \n",
"Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 \n",
"Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 \n",
"Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 \n",
"Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"colnames(df) = colnames(mtcars)\n",
"head(df)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Accessing one data point / one column / row of data\n",
"\n",
"Think the dataframe as a grid and each data point can be located using (i,j) where i is the ith row and j is the jth column. Again remember R indexes start from 1!\n",
"\n",
"\n",
"### Accessing individual data point\n",
"To access the value \"cyl\" (2nd column) of \"Mazda RX5\" (1st row), we can simply:"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"6"
],
"text/latex": [
"6"
],
"text/markdown": [
"6"
],
"text/plain": [
"[1] 6"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df[1,2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want also the \"disp\" (3rd column) and \"hp\" (4th column) as well, we can assess the 3 values by:"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | cyl | disp | hp |
\n",
"\n",
"\tMazda RX5 | 6 | 160 | 110 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lll}\n",
" & cyl & disp & hp\\\\\n",
"\\hline\n",
"\tMazda RX5 & 6 & 160 & 110\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | cyl | disp | hp | \n",
"|---|\n",
"| Mazda RX5 | 6 | 160 | 110 | \n",
"\n",
"\n"
],
"text/plain": [
" cyl disp hp \n",
"Mazda RX5 6 160 110"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df[1,2:4] # this means geting the value from 1 st row and 2 *to* 4th column"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes you may prefer specifying the rows and columns by name instead of indexes:"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | cyl | disp | hp |
\n",
"\n",
"\tMazda RX5 | 6 | 160 | 110 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lll}\n",
" & cyl & disp & hp\\\\\n",
"\\hline\n",
"\tMazda RX5 & 6 & 160 & 110\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | cyl | disp | hp | \n",
"|---|\n",
"| Mazda RX5 | 6 | 160 | 110 | \n",
"\n",
"\n"
],
"text/plain": [
" cyl disp hp \n",
"Mazda RX5 6 160 110"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df[\"Mazda RX5\",c(\"cyl\", \"disp\", \"hp\")]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see, calling by name will give you exactly the same result. Whether calling by name or by index is your personal choice and depends on situation. For example, calling by name is easier to read, but if we change the rownames or colnames then our code will not run (e.g. we change back the first row name to \"Mazda RX4\"). \n",
"\n",
"### Accessing one column/ one row\n",
"If we want to access the whole row of \"Mazda RX4 Wag\" data (2nd row), we can simply:"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
\n",
"\n",
"\tMazda RX4 Wag | 21 | 6 | 160 | 110 | 3.9 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lllllllllll}\n",
" & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\\\\n",
"\\hline\n",
"\tMazda RX4 Wag & 21 & 6 & 160 & 110 & 3.9 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | \n",
"|---|\n",
"| Mazda RX4 Wag | 21 | 6 | 160 | 110 | 3.9 | 2.875 | 17.02 | 0 | 1 | 4 | 4 | \n",
"\n",
"\n"
],
"text/plain": [
" mpg cyl disp hp drat wt qsec vs am gear carb\n",
"Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df[2,]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that leaving the column index means that we want all columns, so you may guess how you can access a column by:"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- 160
\n",
"\t- 160
\n",
"\t- 108
\n",
"\t- 258
\n",
"\t- 360
\n",
"\t- 225
\n",
"\t- 360
\n",
"\t- 146.7
\n",
"\t- 140.8
\n",
"\t- 167.6
\n",
"\t- 167.6
\n",
"\t- 275.8
\n",
"\t- 275.8
\n",
"\t- 275.8
\n",
"\t- 472
\n",
"\t- 460
\n",
"\t- 440
\n",
"\t- 78.7
\n",
"\t- 75.7
\n",
"\t- 71.1
\n",
"\t- 120.1
\n",
"\t- 318
\n",
"\t- 304
\n",
"\t- 350
\n",
"\t- 400
\n",
"\t- 79
\n",
"\t- 120.3
\n",
"\t- 95.1
\n",
"\t- 351
\n",
"\t- 145
\n",
"\t- 301
\n",
"\t- 121
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 160\n",
"\\item 160\n",
"\\item 108\n",
"\\item 258\n",
"\\item 360\n",
"\\item 225\n",
"\\item 360\n",
"\\item 146.7\n",
"\\item 140.8\n",
"\\item 167.6\n",
"\\item 167.6\n",
"\\item 275.8\n",
"\\item 275.8\n",
"\\item 275.8\n",
"\\item 472\n",
"\\item 460\n",
"\\item 440\n",
"\\item 78.7\n",
"\\item 75.7\n",
"\\item 71.1\n",
"\\item 120.1\n",
"\\item 318\n",
"\\item 304\n",
"\\item 350\n",
"\\item 400\n",
"\\item 79\n",
"\\item 120.3\n",
"\\item 95.1\n",
"\\item 351\n",
"\\item 145\n",
"\\item 301\n",
"\\item 121\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 160\n",
"2. 160\n",
"3. 108\n",
"4. 258\n",
"5. 360\n",
"6. 225\n",
"7. 360\n",
"8. 146.7\n",
"9. 140.8\n",
"10. 167.6\n",
"11. 167.6\n",
"12. 275.8\n",
"13. 275.8\n",
"14. 275.8\n",
"15. 472\n",
"16. 460\n",
"17. 440\n",
"18. 78.7\n",
"19. 75.7\n",
"20. 71.1\n",
"21. 120.1\n",
"22. 318\n",
"23. 304\n",
"24. 350\n",
"25. 400\n",
"26. 79\n",
"27. 120.3\n",
"28. 95.1\n",
"29. 351\n",
"30. 145\n",
"31. 301\n",
"32. 121\n",
"\n",
"\n"
],
"text/plain": [
" [1] 160.0 160.0 108.0 258.0 360.0 225.0 360.0 146.7 140.8 167.6 167.6 275.8\n",
"[13] 275.8 275.8 472.0 460.0 440.0 78.7 75.7 71.1 120.1 318.0 304.0 350.0\n",
"[25] 400.0 79.0 120.3 95.1 351.0 145.0 301.0 121.0"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df[,3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"which gives you the whole column 3.\n",
"\n",
"And you should be able to guess what the following lines are doing:"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
\n",
"\n",
"\tMazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
\n",
"\tDatsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
\n",
"\tHornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lllllllllll}\n",
" & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\\\\n",
"\\hline\n",
"\tMazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\\\\n",
"\tDatsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1 \\\\\n",
"\tHornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | \n",
"|---|---|---|\n",
"| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 | \n",
"| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 | \n",
"| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 | \n",
"\n",
"\n"
],
"text/plain": [
" mpg cyl disp hp drat wt qsec vs am gear carb\n",
"Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 \n",
"Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 \n",
"Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
" | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
\n",
"\n",
"\tMazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
\n",
"\tDatsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
\n",
"\tHornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lllllllllll}\n",
" & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\\\\n",
"\\hline\n",
"\tMazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\\\\n",
"\tDatsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1 \\\\\n",
"\tHornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | \n",
"|---|---|---|\n",
"| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 | \n",
"| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 | \n",
"| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 | \n",
"\n",
"\n"
],
"text/plain": [
" mpg cyl disp hp drat wt qsec vs am gear carb\n",
"Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 \n",
"Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 \n",
"Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
" | mpg | cyl | disp |
\n",
"\n",
"\tMazda RX5 | 21.0 | 6 | 160 |
\n",
"\tMazda RX4 Wag | 21.0 | 6 | 160 |
\n",
"\tDatsun 710 | 22.8 | 4 | 108 |
\n",
"\tHornet 4 Drive | 21.4 | 6 | 258 |
\n",
"\tHornet Sportabout | 18.7 | 8 | 360 |
\n",
"\tValiant | 18.1 | 6 | 225 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lll}\n",
" & mpg & cyl & disp\\\\\n",
"\\hline\n",
"\tMazda RX5 & 21.0 & 6 & 160 \\\\\n",
"\tMazda RX4 Wag & 21.0 & 6 & 160 \\\\\n",
"\tDatsun 710 & 22.8 & 4 & 108 \\\\\n",
"\tHornet 4 Drive & 21.4 & 6 & 258 \\\\\n",
"\tHornet Sportabout & 18.7 & 8 & 360 \\\\\n",
"\tValiant & 18.1 & 6 & 225 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | mpg | cyl | disp | \n",
"|---|---|---|---|---|---|\n",
"| Mazda RX5 | 21.0 | 6 | 160 | \n",
"| Mazda RX4 Wag | 21.0 | 6 | 160 | \n",
"| Datsun 710 | 22.8 | 4 | 108 | \n",
"| Hornet 4 Drive | 21.4 | 6 | 258 | \n",
"| Hornet Sportabout | 18.7 | 8 | 360 | \n",
"| Valiant | 18.1 | 6 | 225 | \n",
"\n",
"\n"
],
"text/plain": [
" mpg cyl disp\n",
"Mazda RX5 21.0 6 160 \n",
"Mazda RX4 Wag 21.0 6 160 \n",
"Datsun 710 22.8 4 108 \n",
"Hornet 4 Drive 21.4 6 258 \n",
"Hornet Sportabout 18.7 8 360 \n",
"Valiant 18.1 6 225 "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
" | mpg | cyl | disp |
\n",
"\n",
"\tMazda RX5 | 21.0 | 6 | 160 |
\n",
"\tMazda RX4 Wag | 21.0 | 6 | 160 |
\n",
"\tDatsun 710 | 22.8 | 4 | 108 |
\n",
"\tHornet 4 Drive | 21.4 | 6 | 258 |
\n",
"\tHornet Sportabout | 18.7 | 8 | 360 |
\n",
"\tValiant | 18.1 | 6 | 225 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lll}\n",
" & mpg & cyl & disp\\\\\n",
"\\hline\n",
"\tMazda RX5 & 21.0 & 6 & 160 \\\\\n",
"\tMazda RX4 Wag & 21.0 & 6 & 160 \\\\\n",
"\tDatsun 710 & 22.8 & 4 & 108 \\\\\n",
"\tHornet 4 Drive & 21.4 & 6 & 258 \\\\\n",
"\tHornet Sportabout & 18.7 & 8 & 360 \\\\\n",
"\tValiant & 18.1 & 6 & 225 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | mpg | cyl | disp | \n",
"|---|---|---|---|---|---|\n",
"| Mazda RX5 | 21.0 | 6 | 160 | \n",
"| Mazda RX4 Wag | 21.0 | 6 | 160 | \n",
"| Datsun 710 | 22.8 | 4 | 108 | \n",
"| Hornet 4 Drive | 21.4 | 6 | 258 | \n",
"| Hornet Sportabout | 18.7 | 8 | 360 | \n",
"| Valiant | 18.1 | 6 | 225 | \n",
"\n",
"\n"
],
"text/plain": [
" mpg cyl disp\n",
"Mazda RX5 21.0 6 160 \n",
"Mazda RX4 Wag 21.0 6 160 \n",
"Datsun 710 22.8 4 108 \n",
"Hornet 4 Drive 21.4 6 258 \n",
"Hornet Sportabout 18.7 8 360 \n",
"Valiant 18.1 6 225 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df[2:4,]\n",
"df[c(\"Mazda RX4 Wag\",\"Datsun\",\"Hornet 4 Drive\"), ]\n",
"head(df[,1:3])\n",
"head(df[,c(\"mpg\",\"cyl\",\"disp\")])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There is another way to call a column of df:"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\t- 21
\n",
"\t- 21
\n",
"\t- 22.8
\n",
"\t- 21.4
\n",
"\t- 18.7
\n",
"\t- 18.1
\n",
"\t- 14.3
\n",
"\t- 24.4
\n",
"\t- 22.8
\n",
"\t- 19.2
\n",
"\t- 17.8
\n",
"\t- 16.4
\n",
"\t- 17.3
\n",
"\t- 15.2
\n",
"\t- 10.4
\n",
"\t- 10.4
\n",
"\t- 14.7
\n",
"\t- 32.4
\n",
"\t- 30.4
\n",
"\t- 33.9
\n",
"\t- 21.5
\n",
"\t- 15.5
\n",
"\t- 15.2
\n",
"\t- 13.3
\n",
"\t- 19.2
\n",
"\t- 27.3
\n",
"\t- 26
\n",
"\t- 30.4
\n",
"\t- 15.8
\n",
"\t- 19.7
\n",
"\t- 15
\n",
"\t- 21.4
\n",
"
\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 21\n",
"\\item 21\n",
"\\item 22.8\n",
"\\item 21.4\n",
"\\item 18.7\n",
"\\item 18.1\n",
"\\item 14.3\n",
"\\item 24.4\n",
"\\item 22.8\n",
"\\item 19.2\n",
"\\item 17.8\n",
"\\item 16.4\n",
"\\item 17.3\n",
"\\item 15.2\n",
"\\item 10.4\n",
"\\item 10.4\n",
"\\item 14.7\n",
"\\item 32.4\n",
"\\item 30.4\n",
"\\item 33.9\n",
"\\item 21.5\n",
"\\item 15.5\n",
"\\item 15.2\n",
"\\item 13.3\n",
"\\item 19.2\n",
"\\item 27.3\n",
"\\item 26\n",
"\\item 30.4\n",
"\\item 15.8\n",
"\\item 19.7\n",
"\\item 15\n",
"\\item 21.4\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 21\n",
"2. 21\n",
"3. 22.8\n",
"4. 21.4\n",
"5. 18.7\n",
"6. 18.1\n",
"7. 14.3\n",
"8. 24.4\n",
"9. 22.8\n",
"10. 19.2\n",
"11. 17.8\n",
"12. 16.4\n",
"13. 17.3\n",
"14. 15.2\n",
"15. 10.4\n",
"16. 10.4\n",
"17. 14.7\n",
"18. 32.4\n",
"19. 30.4\n",
"20. 33.9\n",
"21. 21.5\n",
"22. 15.5\n",
"23. 15.2\n",
"24. 13.3\n",
"25. 19.2\n",
"26. 27.3\n",
"27. 26\n",
"28. 30.4\n",
"29. 15.8\n",
"30. 19.7\n",
"31. 15\n",
"32. 21.4\n",
"\n",
"\n"
],
"text/plain": [
" [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4\n",
"[16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7\n",
"[31] 15.0 21.4"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df$mpg"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Accessing row/column with special conditions\n",
"\n",
"What if I want all the information of cars with names starting with \"Mazda\"? How can we proceed?\n",
"\n",
"From the previous section, we would know how to select them **if** we know the indexes for all the cars with names starting with \"Mazda\". Of course you can do it manually..."
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
\n",
"\n",
"\tMazda RX5 | 21 | 6 | 160 | 110 | 3.9 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
\n",
"\tMazda RX4 Wag | 21 | 6 | 160 | 110 | 3.9 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lllllllllll}\n",
" & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\\\\n",
"\\hline\n",
"\tMazda RX5 & 21 & 6 & 160 & 110 & 3.9 & 2.620 & 16.46 & 0 & 1 & 4 & 4 \\\\\n",
"\tMazda RX4 Wag & 21 & 6 & 160 & 110 & 3.9 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | \n",
"|---|---|\n",
"| Mazda RX5 | 21 | 6 | 160 | 110 | 3.9 | 2.620 | 16.46 | 0 | 1 | 4 | 4 | \n",
"| Mazda RX4 Wag | 21 | 6 | 160 | 110 | 3.9 | 2.875 | 17.02 | 0 | 1 | 4 | 4 | \n",
"\n",
"\n"
],
"text/plain": [
" mpg cyl disp hp drat wt qsec vs am gear carb\n",
"Mazda RX5 21 6 160 110 3.9 2.620 16.46 0 1 4 4 \n",
"Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df[1:2,]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But there is a cleverer way to find the indexes by the function [```which```](https://stat.ethz.ch/R-manual/R-devel/library/base/html/which.html)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" [1] \"Mazda\" \"Mazda\" \"Datsu\" \"Horne\" \"Horne\" \"Valia\" \"Duste\" \"Merc \" \"Merc \"\n",
"[10] \"Merc \" \"Merc \" \"Merc \" \"Merc \" \"Merc \" \"Cadil\" \"Linco\" \"Chrys\" \"Fiat \"\n",
"[19] \"Honda\" \"Toyot\" \"Toyot\" \"Dodge\" \"AMC J\" \"Camar\" \"Ponti\" \"Fiat \" \"Porsc\"\n",
"[28] \"Lotus\" \"Ford \" \"Ferra\" \"Maser\" \"Volvo\"\n"
]
}
],
"source": [
"first.5.letters.of.car.names = substring(rownames(df), 1,5) \n",
"print(first.5.letters.of.car.names)\n",
"i = which(first.5.letters.of.car.names == \"Mazda\")"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
\n",
"\n",
"\tMazda RX5 | 21 | 6 | 160 | 110 | 3.9 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
\n",
"\tMazda RX4 Wag | 21 | 6 | 160 | 110 | 3.9 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|lllllllllll}\n",
" & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\\\\n",
"\\hline\n",
"\tMazda RX5 & 21 & 6 & 160 & 110 & 3.9 & 2.620 & 16.46 & 0 & 1 & 4 & 4 \\\\\n",
"\tMazda RX4 Wag & 21 & 6 & 160 & 110 & 3.9 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | \n",
"|---|---|\n",
"| Mazda RX5 | 21 | 6 | 160 | 110 | 3.9 | 2.620 | 16.46 | 0 | 1 | 4 | 4 | \n",
"| Mazda RX4 Wag | 21 | 6 | 160 | 110 | 3.9 | 2.875 | 17.02 | 0 | 1 | 4 | 4 | \n",
"\n",
"\n"
],
"text/plain": [
" mpg cyl disp hp drat wt qsec vs am gear carb\n",
"Mazda RX5 21 6 160 110 3.9 2.620 16.46 0 1 4 4 \n",
"Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df[i,]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"which gives you the same result!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "ir"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "3.4.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}