(Sections 14.3 - 14.4)
Make sure the following packages are attached:
The purrr package is part of the tidyverse. It provides many higher-order functions.
Each higher-order function iterates over a given iterable data structure (vector, data frame, etc.) with the help of another given function.
map() and VariationsSuppose that we want to generate five vectors, each of which consists of a different given number of real numbers, randomly chosen between 0 and 1.
map()map() WorksIn the template above:
.x can be a list or any atomic vector;.f is a function that is to be applied to each element of .x.... consists of other arguments that are supplied as arguments for the .f function.The result is always a list.
What if we want the random real numbers of be between 4 and 8?
No problem!
Try, and see if you can figure out what’s going on:
.x argument is lower_bounds;.f argument is runif();n = 2 becomes the first argument of runif();max = 8 becomes the max argument of runif();min parameter of runif() must iterate over lower_bounds!Use pmap() for this:
pmap() is called .l. This is set to list(how_many, upper_bounds), and they supply values for two arguments of runif().min = 0 Takes care of the min parameter of runif().how_many covers the n parameter, and upper_bounds covers the max parameter..x can be any object that can be coerced to a list. A data frame will do:
When the result can take on a form other than a list, it is possible to use variants of map() such as:
map_int()map_dbl()map_lgl()map_chr()Here are the types of each variable in m111survey:
Here is a statement of whether or not each variable is a factor:
We would like to produce a graph of the number of home-runs hit by each of the top five home-run hitters in the Major Leagues, for each season since 1900.
We need a list in which each element is a data frame covering a single season.
dplyr’s group_split() is our friend:
list<tibble[,3]> [1:126]
$ : tibble [223 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:223] "allenbo01" "baileha01" "barreji01" "barrysh01" ...
..$ yearID : int [1:223] 1900 1900 1900 1900 1900 1900 1900 1900 1900 1900 ...
..$ HR : int [1:223] 0 0 5 1 0 5 2 0 0 0 ...
$ : tibble [421 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:421] "anderjo01" "bakerbo01" "bakerbo01" "barreji01" ...
..$ yearID : int [1:421] 1901 1901 1901 1901 1901 1901 1901 1901 1901 1901 ...
..$ HR : int [1:421] 8 0 0 4 0 1 0 1 8 6 ...
$ : tibble [467 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:467] "adamsjo01" "adkindo01" "altroni01" "anderjo01" ...
..$ yearID : int [1:467] 1902 1902 1902 1902 1902 1902 1902 1902 1902 1902 ...
..$ HR : int [1:467] 0 0 0 4 0 2 0 3 4 3 ...
$ : tibble [436 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:436] "abbated01" "abbotfr01" "adkindo01" "altroni01" ...
..$ yearID : int [1:436] 1903 1903 1903 1903 1903 1903 1903 1903 1903 1903 ...
..$ HR : int [1:436] 1 1 0 0 0 0 2 0 0 0 ...
$ : tibble [432 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:432] "abbated01" "abbotfr01" "altroni01" "amesre01" ...
..$ yearID : int [1:432] 1904 1904 1904 1904 1904 1904 1904 1904 1904 1904 ...
..$ HR : int [1:432] 3 0 1 0 3 0 0 0 0 0 ...
$ : tibble [470 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:470] "abbated01" "abbotfr01" "ablesha01" "adamsri01" ...
..$ yearID : int [1:470] 1905 1905 1905 1905 1905 1905 1905 1905 1905 1905 ...
..$ HR : int [1:470] 3 0 0 0 0 0 0 0 1 0 ...
$ : tibble [553 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:553] "abbotji02" "abstebi01" "adamsba01" "almeira01" ...
..$ yearID : int [1:553] 1906 1906 1906 1906 1906 1906 1906 1906 1906 1906 ...
..$ HR : int [1:553] 0 0 0 0 3 1 0 0 3 0 ...
$ : tibble [513 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:513] "abbated01" "abbotji02" "adamsba01" "almeira01" ...
..$ yearID : int [1:513] 1907 1907 1907 1907 1907 1907 1907 1907 1907 1907 ...
..$ HR : int [1:513] 2 0 0 0 2 2 0 1 1 0 ...
$ : tibble [538 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:538] "abbated01" "alperwh01" "altizda01" "altizda01" ...
..$ yearID : int [1:538] 1908 1908 1908 1908 1908 1908 1908 1908 1908 1908 ...
..$ HR : int [1:538] 1 1 0 0 0 0 0 0 0 0 ...
$ : tibble [641 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:641] "abbated01" "ablesha01" "abstebi01" "adamsba01" ...
..$ yearID : int [1:641] 1909 1909 1909 1909 1909 1909 1909 1909 1909 1909 ...
..$ HR : int [1:641] 1 0 1 0 1 1 0 0 0 0 ...
$ : tibble [676 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:676] "abbated01" "abbated01" "abbotod01" "abstebi01" ...
..$ yearID : int [1:676] 1910 1910 1910 1910 1910 1910 1910 1910 1910 1910 ...
..$ HR : int [1:676] 0 0 0 0 0 0 0 0 0 0 ...
$ : tibble [730 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:730] "ablesha01" "adamsba01" "adamsbe01" "addiske01" ...
..$ yearID : int [1:730] 1911 1911 1911 1911 1911 1911 1911 1911 1911 1911 ...
..$ HR : int [1:730] 0 0 0 0 0 0 0 0 0 0 ...
$ : tibble [741 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:741] "adamsba01" "adamsbe01" "adamswi01" "aglerjo01" ...
..$ yearID : int [1:741] 1912 1912 1912 1912 1912 1912 1912 1912 1912 1912 ...
..$ HR : int [1:741] 0 0 0 0 0 0 0 2 0 1 ...
$ : tibble [737 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:737] "acostme01" "adamsba01" "adamswi01" "agnewsa01" ...
..$ yearID : int [1:737] 1913 1913 1913 1913 1913 1913 1913 1913 1913 1913 ...
..$ HR : int [1:737] 0 0 0 2 2 0 0 1 0 0 ...
$ : tibble [910 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:910] "acostme01" "adamsba01" "adamsda01" "adamska01" ...
..$ yearID : int [1:910] 1914 1914 1914 1914 1914 1914 1914 1914 1914 1914 ...
..$ HR : int [1:910] 0 1 1 0 0 0 0 0 0 0 ...
$ : tibble [900 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:900] "acostme01" "adamsba01" "adamsbe01" "adamsda01" ...
..$ yearID : int [1:900] 1915 1915 1915 1915 1915 1915 1915 1915 1915 1915 ...
..$ HR : int [1:900] 0 0 0 0 0 0 0 0 0 0 ...
$ : tibble [708 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:708] "acostme01" "adamsba01" "adamsbe01" "agnewsa01" ...
..$ yearID : int [1:708] 1916 1916 1916 1916 1916 1916 1916 1916 1916 1916 ...
..$ HR : int [1:708] 0 0 0 0 0 0 0 0 0 0 ...
$ : tibble [623 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:623] "adamsbe01" "agnewsa01" "ainsmed01" "aldrivi01" ...
..$ yearID : int [1:623] 1917 1917 1917 1917 1917 1917 1917 1917 1917 1917 ...
..$ HR : int [1:623] 1 0 0 0 1 0 0 0 0 0 ...
$ : tibble [669 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:669] "acostme01" "acostme01" "adamsba01" "adamsbe01" ...
..$ yearID : int [1:669] 1918 1918 1918 1918 1918 1918 1918 1918 1918 1918 ...
..$ HR : int [1:669] 0 0 0 0 0 0 0 0 0 0 ...
$ : tibble [689 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:689] "abreueu01" "adamsba01" "adamsbe01" "adamswi01" ...
..$ yearID : int [1:689] 1919 1919 1919 1919 1919 1919 1919 1919 1919 1919 ...
..$ HR : int [1:689] 0 0 1 0 0 3 0 0 0 0 ...
$ : tibble [796 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:796] "abreueu01" "acostjo01" "adamsba01" "ainsmed01" ...
..$ yearID : int [1:796] 1920 1920 1920 1920 1920 1920 1920 1920 1920 1920 ...
..$ HR : int [1:796] 0 0 1 1 1 1 0 0 0 0 ...
$ : tibble [819 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:819] "abreueu01" "acostjo01" "adamsba01" "ainsmed01" ...
..$ yearID : int [1:819] 1921 1921 1921 1921 1921 1921 1921 1921 1921 1921 ...
..$ HR : int [1:819] 1 0 0 0 0 0 1 0 1 0 ...
$ : tibble [808 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:808] "acostjo01" "adamsba01" "adamssp01" "ainsmed01" ...
..$ yearID : int [1:808] 1922 1922 1922 1922 1922 1922 1922 1922 1922 1922 ...
..$ HR : int [1:808] 0 1 0 13 0 0 0 0 0 0 ...
$ : tibble [900 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:900] "abramge01" "abreueu01" "adamsba01" "adamssp01" ...
..$ yearID : int [1:900] 1923 1923 1923 1923 1923 1923 1923 1923 1923 1923 ...
..$ HR : int [1:900] 0 0 0 4 0 3 0 0 0 0 ...
$ : tibble [942 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:942] "abreueu01" "adamsba01" "adamssp01" "ainsmed01" ...
..$ yearID : int [1:942] 1924 1924 1924 1924 1924 1924 1924 1924 1924 1924 ...
..$ HR : int [1:942] 1 0 1 0 0 0 0 1 2 2 ...
$ : tibble [935 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:935] "abreueu01" "adamsba01" "adamsbo01" "adamssp01" ...
..$ yearID : int [1:935] 1925 1925 1925 1925 1925 1925 1925 1925 1925 1925 ...
..$ HR : int [1:935] 0 0 0 2 0 0 1 0 0 2 ...
$ : tibble [904 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:904] "adamsba01" "adamssp01" "adamssp02" "aldrivi01" ...
..$ yearID : int [1:904] 1926 1926 1926 1926 1926 1926 1926 1926 1926 1926 ...
..$ HR : int [1:904] 0 0 0 0 0 0 0 1 0 1 ...
$ : tibble [889 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:889] "adamssp01" "adamssp02" "aldrivi01" "alexach01" ...
..$ yearID : int [1:889] 1927 1927 1927 1927 1927 1927 1927 1927 1927 1927 ...
..$ HR : int [1:889] 0 0 0 0 0 2 2 2 0 0 ...
$ : tibble [876 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:876] "adamssp01" "adkingr01" "aldrivi01" "alexach01" ...
..$ yearID : int [1:876] 1928 1928 1928 1928 1928 1928 1928 1928 1928 1928 ...
..$ HR : int [1:876] 0 0 1 1 1 4 1 2 0 0 ...
$ : tibble [839 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:839] "adamssp01" "adkingr01" "akersbi01" "albrith01" ...
..$ yearID : int [1:839] 1929 1929 1929 1929 1929 1929 1929 1929 1929 1929 ...
..$ HR : int [1:839] 0 0 1 0 25 0 1 6 3 1 ...
$ : tibble [813 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:813] "adamssp01" "akersbi01" "alexada01" "alexape01" ...
..$ yearID : int [1:813] 1930 1930 1930 1930 1930 1930 1930 1930 1930 1930 ...
..$ HR : int [1:813] 0 9 20 0 5 3 7 2 0 0 ...
$ : tibble [764 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:764] "adairji01" "adamsbo02" "adamssp01" "akersbi01" ...
..$ yearID : int [1:764] 1931 1931 1931 1931 1931 1931 1931 1931 1931 1931 ...
..$ HR : int [1:764] 0 0 1 0 3 5 1 1 0 0 ...
$ : tibble [1,031 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:1031] "adamsbo02" "adamsem01" "adamssp01" "akersbi01" ...
..$ yearID : int [1:1031] 1932 1932 1932 1932 1932 1932 1932 1932 1932 1932 ...
..$ HR : int [1:1031] 0 0 0 1 0 0 8 0 1 0 ...
$ : tibble [734 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:734] "adamssp01" "adamssp01" "alexada01" "allenet01" ...
..$ yearID : int [1:734] 1933 1933 1933 1933 1933 1933 1933 1933 1933 1933 ...
..$ HR : int [1:734] 0 1 5 0 0 0 1 0 0 0 ...
$ : tibble [758 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:758] "adamssp01" "allenet01" "allenha03" "allenjo02" ...
..$ yearID : int [1:758] 1934 1934 1934 1934 1934 1934 1934 1934 1934 1934 ...
..$ HR : int [1:758] 0 10 0 0 0 0 0 1 2 0 ...
$ : tibble [744 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:744] "adamspa01" "allenet01" "allenjo02" "allenne02" ...
..$ yearID : int [1:744] 1935 1935 1935 1935 1935 1935 1935 1935 1935 1935 ...
..$ HR : int [1:744] 0 8 1 0 3 0 1 0 0 0 ...
$ : tibble [725 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:725] "albrith01" "allenet01" "allenet01" "allenjo02" ...
..$ yearID : int [1:725] 1936 1936 1936 1936 1936 1936 1936 1936 1936 1936 ...
..$ HR : int [1:725] 0 1 3 0 0 1 0 0 5 0 ...
$ : tibble [899 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:899] "adamsem01" "alberjo02" "alexahu01" "allenbo03" ...
..$ yearID : int [1:899] 1937 1937 1937 1937 1937 1937 1937 1937 1937 1937 ...
..$ HR : int [1:899] 0 0 0 0 0 0 0 1 1 1 ...
$ : tibble [919 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:919] "adamsem01" "alexate01" "allencl02" "allenet01" ...
..$ yearID : int [1:919] 1938 1938 1938 1938 1938 1938 1938 1938 1938 1938 ...
..$ HR : int [1:919] 0 0 0 0 1 1 1 3 0 0 ...
$ : tibble [912 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:912] "adamsbu01" "adamsem01" "aderhmo01" "alexate01" ...
..$ yearID : int [1:912] 1939 1939 1939 1939 1939 1939 1939 1939 1939 1939 ...
..$ HR : int [1:912] 0 0 1 0 0 0 1 0 0 0 ...
$ : tibble [898 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:898] "adamsem01" "aderhmo01" "alexasp01" "alexate01" ...
..$ yearID : int [1:898] 1940 1940 1940 1940 1940 1940 1940 1940 1940 1940 ...
..$ HR : int [1:898] 0 0 0 0 0 0 0 0 3 0 ...
$ : tibble [890 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:890] "adamsac01" "adamsem01" "aderhmo01" "albosed01" ...
..$ yearID : int [1:890] 1941 1941 1941 1941 1941 1941 1941 1941 1941 1941 ...
..$ HR : int [1:890] 0 0 0 0 1 1 0 0 1 0 ...
$ : tibble [891 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:891] "abernte01" "abreujo01" "adams01" "adamsac01" ...
..$ yearID : int [1:891] 1942 1942 1942 1942 1942 1942 1942 1942 1942 1942 ...
..$ HR : int [1:891] 0 1 0 0 0 0 0 0 0 0 ...
$ : tibble [920 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:920] "abernte01" "adamsac01" "adamsbu01" "adamsbu01" ...
..$ yearID : int [1:920] 1943 1943 1943 1943 1943 1943 1943 1943 1943 1943 ...
..$ HR : int [1:920] 0 0 0 4 0 0 0 0 0 0 ...
$ : tibble [902 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:902] "abernte01" "adamsac01" "adamsbu01" "aderhmo01" ...
..$ yearID : int [1:902] 1944 1944 1944 1944 1944 1944 1944 1944 1944 1944 ...
..$ HR : int [1:902] 0 0 17 0 0 1 0 0 0 0 ...
$ : tibble [922 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:922] "abernro01" "adamsac01" "adamsbu01" "adamsbu01" ...
..$ yearID : int [1:922] 1945 1945 1945 1945 1945 1945 1945 1945 1945 1945 ...
..$ HR : int [1:922] 0 0 2 20 0 0 2 0 0 0 ...
$ : tibble [1,023 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:1023] "abernwo01" "adamsac01" "adamsbo03" "adamsbu01" ...
..$ yearID : int [1:1023] 1946 1946 1946 1946 1946 1946 1946 1946 1946 1946 ...
..$ HR : int [1:1023] 0 0 4 5 0 0 0 0 0 0 ...
$ : tibble [907 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:907] "abernro01" "abernwo01" "aberscl01" "adamsbo03" ...
..$ yearID : int [1:907] 1947 1947 1947 1947 1947 1947 1947 1947 1947 1947 ...
..$ HR : int [1:907] 0 0 4 4 2 2 0 2 0 0 ...
$ : tibble [869 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:869] "abernro01" "aberscl01" "adamsbo03" "adamshe01" ...
..$ yearID : int [1:869] 1948 1948 1948 1948 1948 1948 1948 1948 1948 1948 ...
..$ HR : int [1:869] 0 1 1 0 0 1 0 0 0 0 ...
$ : tibble [573 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:573] "aberscl01" "abramca01" "adamsbo03" "adamshe01" ...
..$ yearID : int [1:573] 1949 1949 1949 1949 1949 1949 1949 1949 1949 1949 ...
..$ HR : int [1:573] 0 0 0 0 1 0 1 0 5 0 ...
$ : tibble [577 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:577] "aberal01" "abramca01" "adamsbo03" "adamshe01" ...
..$ yearID : int [1:577] 1950 1950 1950 1950 1950 1950 1950 1950 1950 1950 ...
..$ HR : int [1:577] 0 0 3 0 8 0 0 0 0 0 ...
$ : tibble [616 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:616] "abramca01" "adamsbo03" "adcocjo01" "addisbo01" ...
..$ yearID : int [1:616] 1951 1951 1951 1951 1951 1951 1951 1951 1951 1951 ...
..$ HR : int [1:616] 3 5 10 1 0 7 4 2 10 0 ...
$ : tibble [632 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:632] "abernbi01" "abramca01" "abramca01" "adamsbo03" ...
..$ yearID : int [1:632] 1952 1952 1952 1952 1952 1952 1952 1952 1952 1952 ...
..$ HR : int [1:632] 0 0 2 6 13 1 0 0 0 1 ...
$ : tibble [586 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:586] "aberal01" "aberal01" "abramca01" "adamsbo03" ...
..$ yearID : int [1:586] 1953 1953 1953 1953 1953 1953 1953 1953 1953 1953 ...
..$ HR : int [1:586] 0 0 15 8 18 0 0 0 0 1 ...
$ : tibble [576 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:576] "aaronha01" "aberal01" "abramca01" "abramca01" ...
..$ yearID : int [1:576] 1954 1954 1954 1954 1954 1954 1954 1954 1954 1954 ...
..$ HR : int [1:576] 13 0 0 6 3 23 11 3 4 0 ...
$ : tibble [655 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:655] "aaronha01" "aberal01" "abernte02" "abramca01" ...
..$ yearID : int [1:655] 1955 1955 1955 1955 1955 1955 1955 1955 1955 1955 ...
..$ HR : int [1:655] 27 0 0 6 2 0 15 0 0 0 ...
$ : tibble [621 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:621] "aaronha01" "aberal01" "abernte02" "abramca01" ...
..$ yearID : int [1:621] 1956 1956 1956 1956 1956 1956 1956 1956 1956 1956 ...
..$ HR : int [1:621] 26 0 0 0 0 0 38 0 0 16 ...
$ : tibble [615 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:615] "aaronha01" "aberal01" "aberal01" "abernte02" ...
..$ yearID : int [1:615] 1957 1957 1957 1957 1957 1957 1957 1957 1957 1957 ...
..$ HR : int [1:615] 44 0 0 0 0 1 12 0 0 0 ...
$ : tibble [638 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:638] "aaronha01" "ackerto01" "adairje01" "adamsbo03" ...
..$ yearID : int [1:638] 1958 1958 1958 1958 1958 1958 1958 1958 1958 1958 ...
..$ HR : int [1:638] 30 0 0 0 19 0 0 0 4 0 ...
$ : tibble [632 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:632] "aaronha01" "ackerto01" "adairje01" "adamsbo03" ...
..$ yearID : int [1:632] 1959 1959 1959 1959 1959 1959 1959 1959 1959 1959 ...
..$ HR : int [1:632] 39 0 0 0 25 0 30 10 12 0 ...
$ : tibble [637 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:637] "aaronha01" "abernte02" "adairje01" "adcocjo01" ...
..$ yearID : int [1:637] 1960 1960 1960 1960 1960 1960 1960 1960 1960 1960 ...
..$ HR : int [1:637] 40 0 1 25 0 15 8 0 13 0 ...
$ : tibble [698 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:698] "aaronha01" "adairje01" "adcocjo01" "aguirha01" ...
..$ yearID : int [1:698] 1961 1961 1961 1961 1961 1961 1961 1961 1961 1961 ...
..$ HR : int [1:698] 34 9 35 0 0 29 18 6 27 3 ...
$ : tibble [760 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:760] "aaronha01" "aaronto01" "adairje01" "adcocjo01" ...
..$ yearID : int [1:760] 1962 1962 1962 1962 1962 1962 1962 1962 1962 1962 ...
..$ HR : int [1:760] 45 8 11 29 0 0 12 0 29 25 ...
$ : tibble [752 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:752] "aaronha01" "aaronto01" "abernte02" "acklefr01" ...
..$ yearID : int [1:752] 1963 1963 1963 1963 1963 1963 1963 1963 1963 1963 ...
..$ HR : int [1:752] 44 1 0 0 6 13 0 1 0 9 ...
$ : tibble [754 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:754] "aaronha01" "abernte02" "acklefr01" "adairje01" ...
..$ yearID : int [1:754] 1964 1964 1964 1964 1964 1964 1964 1964 1964 1964 ...
..$ HR : int [1:754] 24 0 0 9 21 0 0 0 0 6 ...
$ : tibble [751 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:751] "aaronha01" "aaronto01" "abernte02" "adairje01" ...
..$ yearID : int [1:751] 1965 1965 1965 1965 1965 1965 1965 1965 1965 1965 ...
..$ HR : int [1:751] 32 0 0 7 14 0 0 0 0 0 ...
$ : tibble [774 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:774] "aaronha01" "abernte02" "abernte02" "adairje01" ...
..$ yearID : int [1:774] 1966 1966 1966 1966 1966 1966 1966 1966 1966 1966 ...
..$ HR : int [1:774] 44 0 0 0 4 18 0 22 0 0 ...
$ : tibble [786 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:786] "aaronha01" "abernte02" "adairje01" "adairje01" ...
..$ yearID : int [1:786] 1967 1967 1967 1967 1967 1967 1967 1967 1967 1967 ...
..$ HR : int [1:786] 39 0 0 3 0 1 14 0 0 0 ...
$ : tibble [715 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:715] "aaronha01" "aaronto01" "abernte02" "adairje01" ...
..$ yearID : int [1:715] 1968 1968 1968 1968 1968 1968 1968 1968 1968 1968 ...
..$ HR : int [1:715] 29 1 0 2 0 0 5 0 0 2 ...
$ : tibble [932 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:932] "aaronha01" "aaronto01" "abernte02" "adairje01" ...
..$ yearID : int [1:932] 1969 1969 1969 1969 1969 1969 1969 1969 1969 1969 ...
..$ HR : int [1:932] 44 1 0 5 0 0 26 0 0 0 ...
$ : tibble [919 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:919] "aaronha01" "aaronto01" "abernte02" "abernte02" ...
..$ yearID : int [1:919] 1970 1970 1970 1970 1970 1970 1970 1970 1970 1970 ...
..$ HR : int [1:919] 38 2 0 0 0 0 0 24 0 0 ...
$ : tibble [883 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:883] "aaronha01" "aaronto01" "abernte02" "acosted01" ...
..$ yearID : int [1:883] 1971 1971 1971 1971 1971 1971 1971 1971 1971 1971 ...
..$ HR : int [1:883] 47 0 0 0 14 0 0 4 23 1 ...
$ : tibble [889 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:889] "aaronha01" "abernte02" "acostcy01" "acosted01" ...
..$ yearID : int [1:889] 1972 1972 1972 1972 1972 1972 1972 1972 1972 1972 ...
..$ HR : int [1:889] 34 0 0 0 0 13 0 0 0 9 ...
$ : tibble [892 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:892] "aaronha01" "abbotgl01" "acostcy01" "adamsmi02" ...
..$ yearID : int [1:892] 1973 1973 1973 1973 1973 1973 1973 1973 1973 1973 ...
..$ HR : int [1:892] 40 0 0 3 8 3 0 0 0 0 ...
$ : tibble [914 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:914] "aaronha01" "abbotgl01" "acostcy01" "akerja01" ...
..$ yearID : int [1:914] 1974 1974 1974 1974 1974 1974 1974 1974 1974 1974 ...
..$ HR : int [1:914] 20 0 0 0 0 0 0 0 32 0 ...
$ : tibble [907 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:907] "aaronha01" "abbotgl01" "acostcy01" "adamsgl01" ...
..$ yearID : int [1:907] 1975 1975 1975 1975 1975 1975 1975 1975 1975 1975 ...
..$ HR : int [1:907] 12 0 0 4 0 0 0 0 12 0 ...
$ : tibble [886 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:886] "aaronha01" "abbotgl01" "adamsgl01" "adamsmi02" ...
..$ yearID : int [1:886] 1976 1976 1976 1976 1976 1976 1976 1976 1976 1976 ...
..$ HR : int [1:886] 10 0 0 0 0 0 0 0 2 0 ...
$ : tibble [984 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:984] "aasedo01" "abbotgl01" "adamsbo04" "adamsgl01" ...
..$ yearID : int [1:984] 1977 1977 1977 1977 1977 1977 1977 1977 1977 1977 ...
..$ HR : int [1:984] 0 0 2 6 0 0 0 1 0 5 ...
$ : tibble [960 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:960] "aasedo01" "abbotgl01" "adamsgl01" "adamsmi02" ...
..$ yearID : int [1:960] 1978 1978 1978 1978 1978 1978 1978 1978 1978 1978 ...
..$ HR : int [1:960] 0 0 7 0 0 0 10 17 0 0 ...
$ : tibble [961 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:961] "aasedo01" "abbotgl01" "adamsgl01" "aikenwi01" ...
..$ yearID : int [1:961] 1979 1979 1979 1979 1979 1979 1979 1979 1979 1979 ...
..$ HR : int [1:961] 0 0 8 21 2 0 15 0 0 3 ...
$ : tibble [950 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:950] "aasedo01" "abbotgl01" "adamsgl01" "aguaylu01" ...
..$ yearID : int [1:950] 1980 1980 1980 1980 1980 1980 1980 1980 1980 1980 ...
..$ HR : int [1:950] 0 0 6 1 20 0 0 5 0 0 ...
$ : tibble [944 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:944] "aasedo01" "abbotgl01" "adamsgl01" "agostju01" ...
..$ yearID : int [1:944] 1981 1981 1981 1981 1981 1981 1981 1981 1981 1981 ...
..$ HR : int [1:944] 0 0 2 0 1 17 0 0 1 0 ...
$ : tibble [992 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:992] "aasedo01" "adamsgl01" "adamsri02" "agostju01" ...
..$ yearID : int [1:992] 1982 1982 1982 1982 1982 1982 1982 1982 1982 1982 ...
..$ HR : int [1:992] 0 1 0 0 3 17 0 6 0 4 ...
$ : tibble [1,006 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:1006] "abbotgl01" "abbotgl01" "ackerji01" "adamsri02" ...
..$ yearID : int [1:1006] 1983 1983 1983 1983 1983 1983 1983 1983 1983 1983 ...
..$ HR : int [1:1006] 0 0 0 2 0 0 0 23 0 0 ...
$ : tibble [984 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:984] "aasedo01" "abbotgl01" "ackerji01" "agostju01" ...
..$ yearID : int [1:984] 1984 1984 1984 1984 1984 1984 1984 1984 1984 1984 ...
..$ HR : int [1:984] 0 0 0 0 3 11 0 2 0 0 ...
$ : tibble [998 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:998] "aasedo01" "abregjo01" "ackerji01" "adamsri02" ...
..$ yearID : int [1:998] 1985 1985 1985 1985 1985 1985 1985 1985 1985 1985 ...
..$ HR : int [1:998] 0 0 0 2 0 6 0 1 0 0 ...
$ : tibble [1,017 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:1017] "aasedo01" "ackerji01" "ackerji01" "adducji01" ...
..$ yearID : int [1:1017] 1986 1986 1986 1986 1986 1986 1986 1986 1986 1986 ...
..$ HR : int [1:1017] 0 0 0 0 0 0 4 2 0 2 ...
$ : tibble [1,048 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:1048] "aasedo01" "abnersh01" "ackerji01" "afenitr01" ...
..$ yearID : int [1:1048] 1987 1987 1987 1987 1987 1987 1987 1987 1987 1987 ...
..$ HR : int [1:1048] 0 2 0 0 0 12 1 0 9 0 ...
$ : tibble [1,035 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:1035] "aasedo01" "abnersh01" "ackerji01" "adducji01" ...
..$ yearID : int [1:1035] 1988 1988 1988 1988 1988 1988 1988 1988 1988 1988 ...
..$ HR : int [1:1035] 0 2 0 1 0 3 3 0 0 3 ...
$ : tibble [1,073 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:1073] "aasedo01" "abbotji01" "abnersh01" "ackerji01" ...
..$ yearID : int [1:1073] 1989 1989 1989 1989 1989 1989 1989 1989 1989 1989 ...
..$ HR : int [1:1073] 0 0 2 0 0 0 0 1 0 0 ...
$ : tibble [1,115 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:1115] "aasedo01" "abbotji01" "abbotpa01" "abnersh01" ...
..$ yearID : int [1:1115] 1990 1990 1990 1990 1990 1990 1990 1990 1990 1990 ...
..$ HR : int [1:1115] 0 0 0 1 0 0 0 0 0 0 ...
$ : tibble [1,086 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:1086] "abbotji01" "abbotky01" "abbotpa01" "abnersh01" ...
..$ yearID : int [1:1086] 1991 1991 1991 1991 1991 1991 1991 1991 1991 1991 ...
..$ HR : int [1:1086] 0 0 0 1 2 0 0 0 0 0 ...
$ : tibble [1,066 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:1066] "abbotji01" "abbotky01" "abbotpa01" "abnersh01" ...
..$ yearID : int [1:1066] 1992 1992 1992 1992 1992 1992 1992 1992 1992 1992 ...
..$ HR : int [1:1066] 0 0 0 1 0 0 0 0 0 0 ...
$ : tibble [1,180 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:1180] "abbotji01" "abbotku01" "abbotpa01" "agostju01" ...
..$ yearID : int [1:1180] 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 ...
..$ HR : int [1:1180] 0 3 0 0 0 10 0 0 0 3 ...
$ : tibble [1,030 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:1030] "abbotji01" "abbotku01" "acrema01" "aguilri01" ...
..$ yearID : int [1:1030] 1994 1994 1994 1994 1994 1994 1994 1994 1994 1994 ...
..$ HR : int [1:1030] 0 9 0 0 4 5 8 14 22 0 ...
$ : tibble [1,253 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:1253] "abbotji01" "abbotji01" "abbotku01" "abbotky01" ...
..$ yearID : int [1:1253] 1995 1995 1995 1995 1995 1995 1995 1995 1995 1995 ...
..$ HR : int [1:1253] 0 0 17 0 0 0 0 0 0 0 ...
$ : tibble [1,253 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:1253] "abbotji01" "abbotku01" "abbotky01" "abreubo01" ...
..$ yearID : int [1:1253] 1996 1996 1996 1996 1996 1996 1996 1996 1996 1996 ...
..$ HR : int [1:1253] 0 8 0 0 0 0 0 0 0 0 ...
$ : tibble [1,236 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:1236] "abbotje01" "abbotku01" "abreubo01" "aceveju01" ...
..$ yearID : int [1:1236] 1997 1997 1997 1997 1997 1997 1997 1997 1997 1997 ...
..$ HR : int [1:1236] 1 6 3 0 0 0 0 0 0 0 ...
$ : tibble [1,322 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr [1:1322] "abbotje01" "abbotji01" "abbotku01" "abbotku01" ...
..$ yearID : int [1:1322] 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 ...
..$ HR : int [1:1322] 12 0 2 3 0 17 0 0 0 0 ...
[list output truncated]
@ ptype: tibble [0 × 3] (S3: tbl_df/tbl/data.frame)
..$ playerID: chr(0)
..$ yearID : int(0)
..$ HR : int(0)
keep()keep() is similar to dplyr’s filter(), but whereas filter() chooses rows of a data frame based on a given conditions, keep() chooses the elements of the input list or vector .x based on a condition named .p.
From a vector, keep the numbers that are 1 more than a multiple of 3:
In a data frame, keep the columns that are factor-variables:
discard()discard(.x,, . p = condition) is equivalent to keep(.x, .p = !condition). Thus:
reduce()Given a vector .x and a function .f that takes two inputs, reduce() does the following:
f to elements 1 and 2 of .x, getting a result;f to the result and to element 3 of .x, getting another result;f to this new result and to element 4 of .x, getting yet another result ….x have been exhausted.reduce() returns the final result in the above series of operations.Suppose that you want to add up the elements of the vector:
Of course you could just use:
But we will use reduce().
vec), getting 4;vec, getting 8;vec, getting 14;A common application of reduce is to take an operation that is defined on only two items and extend it to operate on any number of items.
Consider the function intersect():
intersect()You cannot intersect three or more vectors at once:
reduce() to the Rescue!