@@ -99,7 +99,8 @@ str(gapminder)
9999 $ gdpPercap: num 779 821 853 836 740 ...
100100```
101101
102- We can also examine individual columns of the data frame with our ` class ` function:
102+ We can also examine individual columns of the data frame with the ` class ` or
103+ 'typeof' functions:
103104
104105
105106``` r
@@ -110,6 +111,14 @@ class(gapminder$year)
110111[1] "integer"
111112```
112113
114+ ``` r
115+ typeof(gapminder $ year )
116+ ```
117+
118+ ``` output
119+ [1] "integer"
120+ ```
121+
113122``` r
114123class(gapminder $ country )
115124```
@@ -400,6 +409,109 @@ tail(gapminder_norway)
400409```
401410
402411
412+
413+ ## Removing columns and rows in data frames
414+
415+ To remove columns from a data frame, we can use the 'subset' function.
416+ This function allows us to remove columns using their names.
417+ If we want to keep all columns except continent, pop and gdpPercap we can use the following ` subset ` command:
418+
419+
420+ ``` r
421+ life_expectancy <- subset(gapminder , select = - c(continent , pop , gdpPercap ))
422+ head(life_expectancy )
423+ ```
424+
425+ ``` output
426+ country year lifeExp below_average
427+ 1 Afghanistan 1952 28.801 TRUE
428+ 2 Afghanistan 1957 30.332 TRUE
429+ 3 Afghanistan 1962 31.997 TRUE
430+ 4 Afghanistan 1967 34.020 TRUE
431+ 5 Afghanistan 1972 36.088 TRUE
432+ 6 Afghanistan 1977 38.438 TRUE
433+ ```
434+
435+ We can also use a logical vector to achieve the same result. Make sure the
436+ vector's length match the number of columns in the data frame (to avoid R repeating the shorter vector to match the length of the longer vector):
437+
438+
439+ ``` r
440+ life_expectancy <- gapminder [c(TRUE , TRUE , FALSE , FALSE , TRUE , FALSE )]
441+ head(life_expectancy )
442+ ```
443+
444+ ``` output
445+ country year lifeExp below_average
446+ 1 Afghanistan 1952 28.801 TRUE
447+ 2 Afghanistan 1957 30.332 TRUE
448+ 3 Afghanistan 1962 31.997 TRUE
449+ 4 Afghanistan 1967 34.020 TRUE
450+ 5 Afghanistan 1972 36.088 TRUE
451+ 6 Afghanistan 1977 38.438 TRUE
452+ ```
453+
454+ Vector recycling occurs when working with vectors of different length and it
455+ consist on repeating the elements of the shorter vector up to the lenght of
456+ the larger one. For more information, check the book R for Data Science and its
457+ [ chapter about vectors] ( https://r4ds.had.co.nz/vectors.html#scalars-and-recycling-rules ) .
458+
459+ Alternatively, we can use column positions:
460+
461+
462+ ``` r
463+ life_expectancy <- gapminder [- c(3 , 4 , 6 )]
464+ head(life_expectancy )
465+ ```
466+
467+ ``` output
468+ country year lifeExp below_average
469+ 1 Afghanistan 1952 28.801 TRUE
470+ 2 Afghanistan 1957 30.332 TRUE
471+ 3 Afghanistan 1962 31.997 TRUE
472+ 4 Afghanistan 1967 34.020 TRUE
473+ 5 Afghanistan 1972 36.088 TRUE
474+ 6 Afghanistan 1977 38.438 TRUE
475+ ```
476+
477+ Note that typically we select the rows we want to keep, rather than removing rows we do not want in the data.
478+ we want to keep instead.
479+ However, to remove rows from a data frame, we can use their positions:
480+
481+
482+ ``` r
483+ # Filter data for Afghanistan during the 20th century:
484+ afghanistan_20c <- gapminder [gapminder $ country == " Afghanistan" &
485+ gapminder $ year > 2000 , ]
486+
487+ # Now remove data for 2002, that is, the first row:
488+ afghanistan_20c [- 1 , ]
489+ ```
490+
491+ ``` output
492+ country year pop continent lifeExp gdpPercap below_average
493+ 12 Afghanistan 2007 31889923 Asia 43.828 974.5803 TRUE
494+ ```
495+
496+
497+ In research, you may want to remove all the missing data prior to an analysis. Let's first add some missing values (NAs) into the data and then we can use ` na.omit() ` to remove them.
498+
499+
500+ ``` r
501+ # Turn some values into NAs:
502+ afghanistan_20c <- gapminder [gapminder $ country == " Afghanistan" , ]
503+ afghanistan_20c [afghanistan_20c $ year < 2007 , " year" ] <- NA
504+
505+ # Remove NAs
506+ na.omit(afghanistan_20c )
507+ ```
508+
509+ ``` output
510+ country year pop continent lifeExp gdpPercap below_average
511+ 12 Afghanistan 2007 31889923 Asia 43.828 974.5803 TRUE
512+ ```
513+
514+
403515## Factors
404516
405517Here is another thing to look out for: in a ` factor ` , each different value
0 commit comments