Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

This repository contains the course materials from Math 157: Intro to Mathematical Software.

Creative Commons BY-SA 4.0 license.

Views: 3037
License: OTHER
Kernel: SageMath 8.1

Math 157: Intro to Mathematical Software

UC San Diego, winter 2018

February 21, 2018: Introduction to R and statistics (part 1 of 2)

Administrivia:

  • HW 4 has been returned.

  • The solution set for HW 5 has been distributed.

  • The final project will be assigned shortly (target: end of this week). Look for a folder called assignments/2018-03-16 for both parts.

The R Project

The R Project (or just "R" for short) is an open-source project which provides a full programming environment for scientific computation. Although in principle it provides comparable functionality to Mathematica, MATLAB, and Sage, it has become entrenched primarily as a tool for statistics. (It started out as an open-source clone of a system called "S".)

According to this index based on search results, as of this writing R is among the top 15 most widely used programming languages worldwide. It ranks behind Java, C, C++, and Python, but somewhat ahead of MATLAB.

There is a massive R ecosystem; the Comprehensive R Archive Network currently lists more than 12000 packages! The CoCalc installation of R includes some of these packages without any extra download required.

Warning: I am neither a statistician nor a frequent R user. Please bear with me!

Accessing R from Jupyter

There are several ways to access R from Jupyter. If you are using R by itself, then your best bet is to set the kernel to "R (R Project)". Try this now.

cat("Hello world\n") cat("Me too") cat("Me three")
Hello world Me tooMe three
x <- c(2,3,4) # Assignment; by the way, comments start with # just like in Python
x
  1. 2
  2. 3
  3. 4
mean(x)
3
sd(x)
1

The following is an example from the datasets package, which is a collection of data sets. This is an incredible resource for practicing basic concepts of statistics!

sunspots
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 1749 58.0 62.6 70.0 55.7 85.0 83.5 94.8 66.3 75.9 75.5 158.6 85.2 1750 73.3 75.9 89.2 88.3 90.0 100.0 85.4 103.0 91.2 65.7 63.3 75.4 1751 70.0 43.5 45.3 56.4 60.7 50.7 66.3 59.8 23.5 23.2 28.5 44.0 1752 35.0 50.0 71.0 59.3 59.7 39.6 78.4 29.3 27.1 46.6 37.6 40.0 1753 44.0 32.0 45.7 38.0 36.0 31.7 22.2 39.0 28.0 25.0 20.0 6.7 1754 0.0 3.0 1.7 13.7 20.7 26.7 18.8 12.3 8.2 24.1 13.2 4.2 1755 10.2 11.2 6.8 6.5 0.0 0.0 8.6 3.2 17.8 23.7 6.8 20.0 1756 12.5 7.1 5.4 9.4 12.5 12.9 3.6 6.4 11.8 14.3 17.0 9.4 1757 14.1 21.2 26.2 30.0 38.1 12.8 25.0 51.3 39.7 32.5 64.7 33.5 1758 37.6 52.0 49.0 72.3 46.4 45.0 44.0 38.7 62.5 37.7 43.0 43.0 1759 48.3 44.0 46.8 47.0 49.0 50.0 51.0 71.3 77.2 59.7 46.3 57.0 1760 67.3 59.5 74.7 58.3 72.0 48.3 66.0 75.6 61.3 50.6 59.7 61.0 1761 70.0 91.0 80.7 71.7 107.2 99.3 94.1 91.1 100.7 88.7 89.7 46.0 1762 43.8 72.8 45.7 60.2 39.9 77.1 33.8 67.7 68.5 69.3 77.8 77.2 1763 56.5 31.9 34.2 32.9 32.7 35.8 54.2 26.5 68.1 46.3 60.9 61.4 1764 59.7 59.7 40.2 34.4 44.3 30.0 30.0 30.0 28.2 28.0 26.0 25.7 1765 24.0 26.0 25.0 22.0 20.2 20.0 27.0 29.7 16.0 14.0 14.0 13.0 1766 12.0 11.0 36.6 6.0 26.8 3.0 3.3 4.0 4.3 5.0 5.7 19.2 1767 27.4 30.0 43.0 32.9 29.8 33.3 21.9 40.8 42.7 44.1 54.7 53.3 1768 53.5 66.1 46.3 42.7 77.7 77.4 52.6 66.8 74.8 77.8 90.6 111.8 1769 73.9 64.2 64.3 96.7 73.6 94.4 118.6 120.3 148.8 158.2 148.1 112.0 1770 104.0 142.5 80.1 51.0 70.1 83.3 109.8 126.3 104.4 103.6 132.2 102.3 1771 36.0 46.2 46.7 64.9 152.7 119.5 67.7 58.5 101.4 90.0 99.7 95.7 1772 100.9 90.8 31.1 92.2 38.0 57.0 77.3 56.2 50.5 78.6 61.3 64.0 1773 54.6 29.0 51.2 32.9 41.1 28.4 27.7 12.7 29.3 26.3 40.9 43.2 1774 46.8 65.4 55.7 43.8 51.3 28.5 17.5 6.6 7.9 14.0 17.7 12.2 1775 4.4 0.0 11.6 11.2 3.9 12.3 1.0 7.9 3.2 5.6 15.1 7.9 1776 21.7 11.6 6.3 21.8 11.2 19.0 1.0 24.2 16.0 30.0 35.0 40.0 1777 45.0 36.5 39.0 95.5 80.3 80.7 95.0 112.0 116.2 106.5 146.0 157.3 1778 177.3 109.3 134.0 145.0 238.9 171.6 153.0 140.0 171.7 156.3 150.3 105.0 1779 114.7 165.7 118.0 145.0 140.0 113.7 143.0 112.0 111.0 124.0 114.0 110.0 1780 70.0 98.0 98.0 95.0 107.2 88.0 86.0 86.0 93.7 77.0 60.0 58.7 1781 98.7 74.7 53.0 68.3 104.7 97.7 73.5 66.0 51.0 27.3 67.0 35.2 1782 54.0 37.5 37.0 41.0 54.3 38.0 37.0 44.0 34.0 23.2 31.5 30.0 1783 28.0 38.7 26.7 28.3 23.0 25.2 32.2 20.0 18.0 8.0 15.0 10.5 1784 13.0 8.0 11.0 10.0 6.0 9.0 6.0 10.0 10.0 8.0 17.0 14.0 1785 6.5 8.0 9.0 15.7 20.7 26.3 36.3 20.0 32.0 47.2 40.2 27.3 1786 37.2 47.6 47.7 85.4 92.3 59.0 83.0 89.7 111.5 112.3 116.0 112.7 1787 134.7 106.0 87.4 127.2 134.8 99.2 128.0 137.2 157.3 157.0 141.5 174.0 1788 138.0 129.2 143.3 108.5 113.0 154.2 141.5 136.0 141.0 142.0 94.7 129.5 1789 114.0 125.3 120.0 123.3 123.5 120.0 117.0 103.0 112.0 89.7 134.0 135.5 1790 103.0 127.5 96.3 94.0 93.0 91.0 69.3 87.0 77.3 84.3 82.0 74.0 1791 72.7 62.0 74.0 77.2 73.7 64.2 71.0 43.0 66.5 61.7 67.0 66.0 1792 58.0 64.0 63.0 75.7 62.0 61.0 45.8 60.0 59.0 59.0 57.0 56.0 1793 56.0 55.0 55.5 53.0 52.3 51.0 50.0 29.3 24.0 47.0 44.0 45.7 1794 45.0 44.0 38.0 28.4 55.7 41.5 41.0 40.0 11.1 28.5 67.4 51.4 1795 21.4 39.9 12.6 18.6 31.0 17.1 12.9 25.7 13.5 19.5 25.0 18.0 1796 22.0 23.8 15.7 31.7 21.0 6.7 26.9 1.5 18.4 11.0 8.4 5.1 1797 14.4 4.2 4.0 4.0 7.3 11.1 4.3 6.0 5.7 6.9 5.8 3.0 1798 2.0 4.0 12.4 1.1 0.0 0.0 0.0 3.0 2.4 1.5 12.5 9.9 1799 1.6 12.6 21.7 8.4 8.2 10.6 2.1 0.0 0.0 4.6 2.7 8.6 1800 6.9 9.3 13.9 0.0 5.0 23.7 21.0 19.5 11.5 12.3 10.5 40.1 1801 27.0 29.0 30.0 31.0 32.0 31.2 35.0 38.7 33.5 32.6 39.8 48.2 1802 47.8 47.0 40.8 42.0 44.0 46.0 48.0 50.0 51.8 38.5 34.5 50.0 1803 50.0 50.8 29.5 25.0 44.3 36.0 48.3 34.1 45.3 54.3 51.0 48.0 1804 45.3 48.3 48.0 50.6 33.4 34.8 29.8 43.1 53.0 62.3 61.0 60.0 1805 61.0 44.1 51.4 37.5 39.0 40.5 37.6 42.7 44.4 29.4 41.0 38.3 1806 39.0 29.6 32.7 27.7 26.4 25.6 30.0 26.3 24.0 27.0 25.0 24.0 1807 12.0 12.2 9.6 23.8 10.0 12.0 12.7 12.0 5.7 8.0 2.6 0.0 1808 0.0 4.5 0.0 12.3 13.5 13.5 6.7 8.0 11.7 4.7 10.5 12.3 1809 7.2 9.2 0.9 2.5 2.0 7.7 0.3 0.2 0.4 0.0 0.0 0.0 1810 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1811 0.0 0.0 0.0 0.0 0.0 0.0 6.6 0.0 2.4 6.1 0.8 1.1 1812 11.3 1.9 0.7 0.0 1.0 1.3 0.5 15.6 5.2 3.9 7.9 10.1 1813 0.0 10.3 1.9 16.6 5.5 11.2 18.3 8.4 15.3 27.8 16.7 14.3 1814 22.2 12.0 5.7 23.8 5.8 14.9 18.5 2.3 8.1 19.3 14.5 20.1 1815 19.2 32.2 26.2 31.6 9.8 55.9 35.5 47.2 31.5 33.5 37.2 65.0 1816 26.3 68.8 73.7 58.8 44.3 43.6 38.8 23.2 47.8 56.4 38.1 29.9 1817 36.4 57.9 96.2 26.4 21.2 40.0 50.0 45.0 36.7 25.6 28.9 28.4 1818 34.9 22.4 25.4 34.5 53.1 36.4 28.0 31.5 26.1 31.7 10.9 25.8 1819 32.5 20.7 3.7 20.2 19.6 35.0 31.4 26.1 14.9 27.5 25.1 30.6 1820 19.2 26.6 4.5 19.4 29.3 10.8 20.6 25.9 5.2 9.0 7.9 9.7 1821 21.5 4.3 5.7 9.2 1.7 1.8 2.5 4.8 4.4 18.8 4.4 0.0 1822 0.0 0.9 16.1 13.5 1.5 5.6 7.9 2.1 0.0 0.4 0.0 0.0 1823 0.0 0.0 0.6 0.0 0.0 0.0 0.5 0.0 0.0 0.0 0.0 20.4 1824 21.6 10.8 0.0 19.4 2.8 0.0 0.0 1.4 20.5 25.2 0.0 0.8 1825 5.0 15.5 22.4 3.8 15.4 15.4 30.9 25.4 15.7 15.6 11.7 22.0 1826 17.7 18.2 36.7 24.0 32.4 37.1 52.5 39.6 18.9 50.6 39.5 68.1 1827 34.6 47.4 57.8 46.0 56.3 56.7 42.9 53.7 49.6 57.2 48.2 46.1 1828 52.8 64.4 65.0 61.1 89.1 98.0 54.3 76.4 50.4 54.7 57.0 46.6 1829 43.0 49.4 72.3 95.0 67.5 73.9 90.8 78.3 52.8 57.2 67.6 56.5 1830 52.2 72.1 84.6 107.1 66.3 65.1 43.9 50.7 62.1 84.4 81.2 82.1 1831 47.5 50.1 93.4 54.6 38.1 33.4 45.2 54.9 37.9 46.2 43.5 28.9 1832 30.9 55.5 55.1 26.9 41.3 26.7 13.9 8.9 8.2 21.1 14.3 27.5 1833 11.3 14.9 11.8 2.8 12.9 1.0 7.0 5.7 11.6 7.5 5.9 9.9 1834 4.9 18.1 3.9 1.4 8.8 7.8 8.7 4.0 11.5 24.8 30.5 34.5 1835 7.5 24.5 19.7 61.5 43.6 33.2 59.8 59.0 100.8 95.2 100.0 77.5 1836 88.6 107.6 98.1 142.9 111.4 124.7 116.7 107.8 95.1 137.4 120.9 206.2 1837 188.0 175.6 134.6 138.2 111.3 158.0 162.8 134.0 96.3 123.7 107.0 129.8 1838 144.9 84.8 140.8 126.6 137.6 94.5 108.2 78.8 73.6 90.8 77.4 79.8 1839 107.6 102.5 77.7 61.8 53.8 54.6 84.7 131.2 132.7 90.8 68.8 63.6 1840 81.2 87.7 55.5 65.9 69.2 48.5 60.7 57.8 74.0 49.8 54.3 53.7 1841 24.0 29.9 29.7 42.6 67.4 55.7 30.8 39.3 35.1 28.5 19.8 38.8 1842 20.4 22.1 21.7 26.9 24.9 20.5 12.6 26.5 18.5 38.1 40.5 17.6 1843 13.3 3.5 8.3 8.8 21.1 10.5 9.5 11.8 4.2 5.3 19.1 12.7 1844 9.4 14.7 13.6 20.8 12.0 3.7 21.2 23.9 6.9 21.5 10.7 21.6 1845 25.7 43.6 43.3 56.9 47.8 31.1 30.6 32.3 29.6 40.7 39.4 59.7 1846 38.7 51.0 63.9 69.2 59.9 65.1 46.5 54.8 107.1 55.9 60.4 65.5 1847 62.6 44.9 85.7 44.7 75.4 85.3 52.2 140.6 161.2 180.4 138.9 109.6 1848 159.1 111.8 108.9 107.1 102.2 123.8 139.2 132.5 100.3 132.4 114.6 159.9 1849 156.7 131.7 96.5 102.5 80.6 81.2 78.0 61.3 93.7 71.5 99.7 97.0 1850 78.0 89.4 82.6 44.1 61.6 70.0 39.1 61.6 86.2 71.0 54.8 60.0 1851 75.5 105.4 64.6 56.5 62.6 63.2 36.1 57.4 67.9 62.5 50.9 71.4 1852 68.4 67.5 61.2 65.4 54.9 46.9 42.0 39.7 37.5 67.3 54.3 45.4 1853 41.1 42.9 37.7 47.6 34.7 40.0 45.9 50.4 33.5 42.3 28.8 23.4 1854 15.4 20.0 20.7 26.4 24.0 21.1 18.7 15.8 22.4 12.7 28.2 21.4 1855 12.3 11.4 17.4 4.4 9.1 5.3 0.4 3.1 0.0 9.7 4.3 3.1 1856 0.5 4.9 0.4 6.5 0.0 5.0 4.6 5.9 4.4 4.5 7.7 7.2 1857 13.7 7.4 5.2 11.1 29.2 16.0 22.2 16.9 42.4 40.6 31.4 37.2 1858 39.0 34.9 57.5 38.3 41.4 44.5 56.7 55.3 80.1 91.2 51.9 66.9 1859 83.7 87.6 90.3 85.7 91.0 87.1 95.2 106.8 105.8 114.6 97.2 81.0 1860 81.5 88.0 98.9 71.4 107.1 108.6 116.7 100.3 92.2 90.1 97.9 95.6 1861 62.3 77.8 101.0 98.5 56.8 87.8 78.0 82.5 79.9 67.2 53.7 80.5 1862 63.1 64.5 43.6 53.7 64.4 84.0 73.4 62.5 66.6 42.0 50.6 40.9 1863 48.3 56.7 66.4 40.6 53.8 40.8 32.7 48.1 22.0 39.9 37.7 41.2 1864 57.7 47.1 66.3 35.8 40.6 57.8 54.7 54.8 28.5 33.9 57.6 28.6 1865 48.7 39.3 39.5 29.4 34.5 33.6 26.8 37.8 21.6 17.1 24.6 12.8 1866 31.6 38.4 24.6 17.6 12.9 16.5 9.3 12.7 7.3 14.1 9.0 1.5 1867 0.0 0.7 9.2 5.1 2.9 1.5 5.0 4.9 9.8 13.5 9.3 25.2 1868 15.6 15.8 26.5 36.6 26.7 31.1 28.6 34.4 43.8 61.7 59.1 67.6 1869 60.9 59.3 52.7 41.0 104.0 108.4 59.2 79.6 80.6 59.4 77.4 104.3 1870 77.3 114.9 159.4 160.0 176.0 135.6 132.4 153.8 136.0 146.4 147.5 130.0 1871 88.3 125.3 143.2 162.4 145.5 91.7 103.0 110.0 80.3 89.0 105.4 90.3 1872 79.5 120.1 88.4 102.1 107.6 109.9 105.5 92.9 114.6 103.5 112.0 83.9 1873 86.7 107.0 98.3 76.2 47.9 44.8 66.9 68.2 47.5 47.4 55.4 49.2 1874 60.8 64.2 46.4 32.0 44.6 38.2 67.8 61.3 28.0 34.3 28.9 29.3 1875 14.6 22.2 33.8 29.1 11.5 23.9 12.5 14.6 2.4 12.7 17.7 9.9 1876 14.3 15.0 31.2 2.3 5.1 1.6 15.2 8.8 9.9 14.3 9.9 8.2 1877 24.4 8.7 11.7 15.8 21.2 13.4 5.9 6.3 16.4 6.7 14.5 2.3 1878 3.3 6.0 7.8 0.1 5.8 6.4 0.1 0.0 5.3 1.1 4.1 0.5 1879 0.8 0.6 0.0 6.2 2.4 4.8 7.5 10.7 6.1 12.3 12.9 7.2 1880 24.0 27.5 19.5 19.3 23.5 34.1 21.9 48.1 66.0 43.0 30.7 29.6 1881 36.4 53.2 51.5 51.7 43.5 60.5 76.9 58.0 53.2 64.0 54.8 47.3 1882 45.0 69.3 67.5 95.8 64.1 45.2 45.4 40.4 57.7 59.2 84.4 41.8 1883 60.6 46.9 42.8 82.1 32.1 76.5 80.6 46.0 52.6 83.8 84.5 75.9 1884 91.5 86.9 86.8 76.1 66.5 51.2 53.1 55.8 61.9 47.8 36.6 47.2 1885 42.8 71.8 49.8 55.0 73.0 83.7 66.5 50.0 39.6 38.7 33.3 21.7 1886 29.9 25.9 57.3 43.7 30.7 27.1 30.3 16.9 21.4 8.6 0.3 12.4 1887 10.3 13.2 4.2 6.9 20.0 15.7 23.3 21.4 7.4 6.6 6.9 20.7 1888 12.7 7.1 7.8 5.1 7.0 7.1 3.1 2.8 8.8 2.1 10.7 6.7 1889 0.8 8.5 7.0 4.3 2.4 6.4 9.7 20.6 6.5 2.1 0.2 6.7 1890 5.3 0.6 5.1 1.6 4.8 1.3 11.6 8.5 17.2 11.2 9.6 7.8 1891 13.5 22.2 10.4 20.5 41.1 48.3 58.8 33.2 53.8 51.5 41.9 32.3 1892 69.1 75.6 49.9 69.6 79.6 76.3 76.8 101.4 62.8 70.5 65.4 78.6 1893 75.0 73.0 65.7 88.1 84.7 88.2 88.8 129.2 77.9 79.7 75.1 93.8 1894 83.2 84.6 52.3 81.6 101.2 98.9 106.0 70.3 65.9 75.5 56.6 60.0 1895 63.3 67.2 61.0 76.9 67.5 71.5 47.8 68.9 57.7 67.9 47.2 70.7 1896 29.0 57.4 52.0 43.8 27.7 49.0 45.0 27.2 61.3 28.4 38.0 42.6 1897 40.6 29.4 29.1 31.0 20.0 11.3 27.6 21.8 48.1 14.3 8.4 33.3 1898 30.2 36.4 38.3 14.5 25.8 22.3 9.0 31.4 34.8 34.4 30.9 12.6 1899 19.5 9.2 18.1 14.2 7.7 20.5 13.5 2.9 8.4 13.0 7.8 10.5 1900 9.4 13.6 8.6 16.0 15.2 12.1 8.3 4.3 8.3 12.9 4.5 0.3 1901 0.2 2.4 4.5 0.0 10.2 5.8 0.7 1.0 0.6 3.7 3.8 0.0 1902 5.2 0.0 12.4 0.0 2.8 1.4 0.9 2.3 7.6 16.3 10.3 1.1 1903 8.3 17.0 13.5 26.1 14.6 16.3 27.9 28.8 11.1 38.9 44.5 45.6 1904 31.6 24.5 37.2 43.0 39.5 41.9 50.6 58.2 30.1 54.2 38.0 54.6 1905 54.8 85.8 56.5 39.3 48.0 49.0 73.0 58.8 55.0 78.7 107.2 55.5 1906 45.5 31.3 64.5 55.3 57.7 63.2 103.6 47.7 56.1 17.8 38.9 64.7 1907 76.4 108.2 60.7 52.6 42.9 40.4 49.7 54.3 85.0 65.4 61.5 47.3 1908 39.2 33.9 28.7 57.6 40.8 48.1 39.5 90.5 86.9 32.3 45.5 39.5 1909 56.7 46.6 66.3 32.3 36.0 22.6 35.8 23.1 38.8 58.4 55.8 54.2 1910 26.4 31.5 21.4 8.4 22.2 12.3 14.1 11.5 26.2 38.3 4.9 5.8 1911 3.4 9.0 7.8 16.5 9.0 2.2 3.5 4.0 4.0 2.6 4.2 2.2 1912 0.3 0.0 4.9 4.5 4.4 4.1 3.0 0.3 9.5 4.6 1.1 6.4 1913 2.3 2.9 0.5 0.9 0.0 0.0 1.7 0.2 1.2 3.1 0.7 3.8 1914 2.8 2.6 3.1 17.3 5.2 11.4 5.4 7.7 12.7 8.2 16.4 22.3 1915 23.0 42.3 38.8 41.3 33.0 68.8 71.6 69.6 49.5 53.5 42.5 34.5 1916 45.3 55.4 67.0 71.8 74.5 67.7 53.5 35.2 45.1 50.7 65.6 53.0 1917 74.7 71.9 94.8 74.7 114.1 114.9 119.8 154.5 129.4 72.2 96.4 129.3 1918 96.0 65.3 72.2 80.5 76.7 59.4 107.6 101.7 79.9 85.0 83.4 59.2 1919 48.1 79.5 66.5 51.8 88.1 111.2 64.7 69.0 54.7 52.8 42.0 34.9 1920 51.1 53.9 70.2 14.8 33.3 38.7 27.5 19.2 36.3 49.6 27.2 29.9 1921 31.5 28.3 26.7 32.4 22.2 33.7 41.9 22.8 17.8 18.2 17.8 20.3 1922 11.8 26.4 54.7 11.0 8.0 5.8 10.9 6.5 4.7 6.2 7.4 17.5 1923 4.5 1.5 3.3 6.1 3.2 9.1 3.5 0.5 13.2 11.6 10.0 2.8 1924 0.5 5.1 1.8 11.3 20.8 24.0 28.1 19.3 25.1 25.6 22.5 16.5 1925 5.5 23.2 18.0 31.7 42.8 47.5 38.5 37.9 60.2 69.2 58.6 98.6 1926 71.8 70.0 62.5 38.5 64.3 73.5 52.3 61.6 60.8 71.5 60.5 79.4 1927 81.6 93.0 69.6 93.5 79.1 59.1 54.9 53.8 68.4 63.1 67.2 45.2 1928 83.5 73.5 85.4 80.6 76.9 91.4 98.0 83.8 89.7 61.4 50.3 59.0 1929 68.9 64.1 50.2 52.8 58.2 71.9 70.2 65.8 34.4 54.0 81.1 108.0 1930 65.3 49.2 35.0 38.2 36.8 28.8 21.9 24.9 32.1 34.4 35.6 25.8 1931 14.6 43.1 30.0 31.2 24.6 15.3 17.4 13.0 19.0 10.0 18.7 17.8 1932 12.1 10.6 11.2 11.2 17.9 22.2 9.6 6.8 4.0 8.9 8.2 11.0 1933 12.3 22.2 10.1 2.9 3.2 5.2 2.8 0.2 5.1 3.0 0.6 0.3 1934 3.4 7.8 4.3 11.3 19.7 6.7 9.3 8.3 4.0 5.7 8.7 15.4 1935 18.9 20.5 23.1 12.2 27.3 45.7 33.9 30.1 42.1 53.2 64.2 61.5 1936 62.8 74.3 77.1 74.9 54.6 70.0 52.3 87.0 76.0 89.0 115.4 123.4 1937 132.5 128.5 83.9 109.3 116.7 130.3 145.1 137.7 100.7 124.9 74.4 88.8 1938 98.4 119.2 86.5 101.0 127.4 97.5 165.3 115.7 89.6 99.1 122.2 92.7 1939 80.3 77.4 64.6 109.1 118.3 101.0 97.6 105.8 112.6 88.1 68.1 42.1 1940 50.5 59.4 83.3 60.7 54.4 83.9 67.5 105.5 66.5 55.0 58.4 68.3 1941 45.6 44.5 46.4 32.8 29.5 59.8 66.9 60.0 65.9 46.3 38.3 33.7 1942 35.6 52.8 54.2 60.7 25.0 11.4 17.7 20.2 17.2 19.2 30.7 22.5 1943 12.4 28.9 27.4 26.1 14.1 7.6 13.2 19.4 10.0 7.8 10.2 18.8 1944 3.7 0.5 11.0 0.3 2.5 5.0 5.0 16.7 14.3 16.9 10.8 28.4 1945 18.5 12.7 21.5 32.0 30.6 36.2 42.6 25.9 34.9 68.8 46.0 27.4 1946 47.6 86.2 76.6 75.7 84.9 73.5 116.2 107.2 94.4 102.3 123.8 121.7 1947 115.7 113.4 129.8 149.8 201.3 163.9 157.9 188.8 169.4 163.6 128.0 116.5 1948 108.5 86.1 94.8 189.7 174.0 167.8 142.2 157.9 143.3 136.3 95.8 138.0 1949 119.1 182.3 157.5 147.0 106.2 121.7 125.8 123.8 145.3 131.6 143.5 117.6 1950 101.6 94.8 109.7 113.4 106.2 83.6 91.0 85.2 51.3 61.4 54.8 54.1 1951 59.9 59.9 59.9 92.9 108.5 100.6 61.5 61.0 83.1 51.6 52.4 45.8 1952 40.7 22.7 22.0 29.1 23.4 36.4 39.3 54.9 28.2 23.8 22.1 34.3 1953 26.5 3.9 10.0 27.8 12.5 21.8 8.6 23.5 19.3 8.2 1.6 2.5 1954 0.2 0.5 10.9 1.8 0.8 0.2 4.8 8.4 1.5 7.0 9.2 7.6 1955 23.1 20.8 4.9 11.3 28.9 31.7 26.7 40.7 42.7 58.5 89.2 76.9 1956 73.6 124.0 118.4 110.7 136.6 116.6 129.1 169.6 173.2 155.3 201.3 192.1 1957 165.0 130.2 157.4 175.2 164.6 200.7 187.2 158.0 235.8 253.8 210.9 239.4 1958 202.5 164.9 190.7 196.0 175.3 171.5 191.4 200.2 201.2 181.5 152.3 187.6 1959 217.4 143.1 185.7 163.3 172.0 168.7 149.6 199.6 145.2 111.4 124.0 125.0 1960 146.3 106.0 102.2 122.0 119.6 110.2 121.7 134.1 127.2 82.8 89.6 85.6 1961 57.9 46.1 53.0 61.4 51.0 77.4 70.2 55.9 63.6 37.7 32.6 40.0 1962 38.7 50.3 45.6 46.4 43.7 42.0 21.8 21.8 51.3 39.5 26.9 23.2 1963 19.8 24.4 17.1 29.3 43.0 35.9 19.6 33.2 38.8 35.3 23.4 14.9 1964 15.3 17.7 16.5 8.6 9.5 9.1 3.1 9.3 4.7 6.1 7.4 15.1 1965 17.5 14.2 11.7 6.8 24.1 15.9 11.9 8.9 16.8 20.1 15.8 17.0 1966 28.2 24.4 25.3 48.7 45.3 47.7 56.7 51.2 50.2 57.2 57.2 70.4 1967 110.9 93.6 111.8 69.5 86.5 67.3 91.5 107.2 76.8 88.2 94.3 126.4 1968 121.8 111.9 92.2 81.2 127.2 110.3 96.1 109.3 117.2 107.7 86.0 109.8 1969 104.4 120.5 135.8 106.8 120.0 106.0 96.8 98.0 91.3 95.7 93.5 97.9 1970 111.5 127.8 102.9 109.5 127.5 106.8 112.5 93.0 99.5 86.6 95.2 83.5 1971 91.3 79.0 60.7 71.8 57.5 49.8 81.0 61.4 50.2 51.7 63.2 82.2 1972 61.5 88.4 80.1 63.2 80.5 88.0 76.5 76.8 64.0 61.3 41.6 45.3 1973 43.4 42.9 46.0 57.7 42.4 39.5 23.1 25.6 59.3 30.7 23.9 23.3 1974 27.6 26.0 21.3 40.3 39.5 36.0 55.8 33.6 40.2 47.1 25.0 20.5 1975 18.9 11.5 11.5 5.1 9.0 11.4 28.2 39.7 13.9 9.1 19.4 7.8 1976 8.1 4.3 21.9 18.8 12.4 12.2 1.9 16.4 13.5 20.6 5.2 15.3 1977 16.4 23.1 8.7 12.9 18.6 38.5 21.4 30.1 44.0 43.8 29.1 43.2 1978 51.9 93.6 76.5 99.7 82.7 95.1 70.4 58.1 138.2 125.1 97.9 122.7 1979 166.6 137.5 138.0 101.5 134.4 149.5 159.4 142.2 188.4 186.2 183.3 176.3 1980 159.6 155.0 126.2 164.1 179.9 157.3 136.3 135.4 155.0 164.7 147.9 174.4 1981 114.0 141.3 135.5 156.4 127.5 90.0 143.8 158.7 167.3 162.4 137.5 150.1 1982 111.2 163.6 153.8 122.0 82.2 110.4 106.1 107.6 118.8 94.7 98.1 127.0 1983 84.3 51.0 66.5 80.7 99.2 91.1 82.2 71.8 50.3 55.8 33.3 33.4
plot?
plot(sunspots, main = "sunspots data", xlab = "Year", ylab = "Monthly sunspot numbers")
Image in a Jupyter notebook

If you are using Python, you can use the rpy2 package. This allows you to perform R operations "within Python", so that you can migrate results in and out of Python. (Note: rpy2 is not included in a standard Python install, but CoCalc provides it.)

import rpy2.robjects as robjects
x = robjects.r("c(2,3,4)") # Run an R command directly
x
R object with classes: ('numeric',) mapped to: <FloatVector - Python:0x7f335bdb3758 / R:0x77b43c0> [2.000000, 3.000000, 4.000000]
robjects.r.mean(x) # Apply an R command to an R object
R object with classes: ('numeric',) mapped to: <FloatVector - Python:0x7f335bdbe050 / R:0x78997b8> [3.000000]
robjects.r.sd(x)
R object with classes: ('numeric',) mapped to: <FloatVector - Python:0x7f335bae3368 / R:0x78a70a8> [1.000000]

This isn't a perfect solution, though.

robjects.r.plot(sunspots, main = "sunspots data", xlab = "Year", ylab = "Monthly sunspot numbers") # This won't work.
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-7-e54ef377d9fd> in <module>() ----> 1 robjects.r.plot(sunspots, main = "sunspots data", xlab = "Year", ylab = "Monthly sunspot numbers") # This won't work. NameError: name 'sunspots' is not defined
robjects.r.plot(robjects.r.sunspots, main = "sunspots data", xlab = "Year", ylab = "Monthly sunspot numbers") # This "works" but we can't see the result.
rpy2.rinterface.NULL

One can easily transfer information from an R array into Python, using Python syntax.

x[1] # Use Python syntax to index into an R array
3.0
len(robjects.r.sunspots)
2820

It's not quite as obvious how to go the other way...

robjects.r([1,2,3])
/ext/sage/sage-8.1/local/lib/python2.7/site-packages/rpy2/rinterface/__init__.py:185: RRuntimeWarning: Error in (function (file = "", n = NULL, text = NULL, prompt = "?", keep.source = getOption("keep.source"), : <text>:1:1: unexpected '[' 1: [ ^ warnings.warn(x, RRuntimeWarning)
--------------------------------------------------------------------------- RRuntimeError Traceback (most recent call last) <ipython-input-11-5b150eebabbf> in <module>() ----> 1 robjects.r([Integer(1),Integer(2),Integer(3)]) /ext/sage/sage-8.1/local/lib/python2.7/site-packages/rpy2/robjects/__init__.pyc in __call__(self, string) 356 357 def __call__(self, string): --> 358 p = _rparse(text=StrSexpVector((string,))) 359 res = self.eval(p) 360 return conversion.ri2py(res) RRuntimeError: Error in (function (file = "", n = NULL, text = NULL, prompt = "?", keep.source = getOption("keep.source"), : <text>:1:1: unexpected '[' 1: [ ^
import numpy
robjects.r(numpy.array([2,3,4]))
--------------------------------------------------------------------------- RRuntimeError Traceback (most recent call last) <ipython-input-13-c091c3840841> in <module>() ----> 1 robjects.r(numpy.array([Integer(2),Integer(3),Integer(4)])) /ext/sage/sage-8.1/local/lib/python2.7/site-packages/rpy2/robjects/__init__.pyc in __call__(self, string) 356 357 def __call__(self, string): --> 358 p = _rparse(text=StrSexpVector((string,))) 359 res = self.eval(p) 360 return conversion.ri2py(res) RRuntimeError: Error in (function (file = "", n = NULL, text = NULL, prompt = "?", keep.source = getOption("keep.source"), : <text>:1:1: unexpected '[' 1: [ ^
robjects.r(x) # This won't work.
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-14-5e60702c68fd> in <module>() ----> 1 robjects.r(x) # This won't work. /ext/sage/sage-8.1/local/lib/python2.7/site-packages/rpy2/robjects/__init__.pyc in __call__(self, string) 356 357 def __call__(self, string): --> 358 p = _rparse(text=StrSexpVector((string,))) 359 res = self.eval(p) 360 return conversion.ri2py(res) ValueError: Error raised when calling str() for element 0.

... because R needs a bit more coaching than Python.

robjects.FloatVector([2,3,5])
R object with classes: ('numeric',) mapped to: <FloatVector - Python:0x7f3357ff5680 / R:0x75e2438> [2.000000, 3.000000, 5.000000]
robjects.DataFrame?

This should all work in SageMath also. Try switching the kernel to "SageMath 8.1" and see for yourself!

rpy2 also provides a gadget that lets you switch back and forth between Python and R at the level of individual cells.

%load_ext rpy2.ipython
%%R y <- c(2,3,4) sd(y)
[1] 1
y
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-3-009520053b00> in <module>() ----> 1 y NameError: name 'y' is not defined
import rpy2.robjects as robjects
x = robjects.r.y x
array([ 2., 3., 4.])
robjects.r.mean(x)
R object with classes: ('numeric',) mapped to: <FloatVector - Python:0x7fad0e815a28 / R:0x6ab8348> [3.000000]
%%R plot(sunspots, main = "sunspots data", xlab = "Year", ylab = "Monthly sunspot numbers") # Will this work?
Image in a Jupyter notebook

There is also a version of this where one switches into R for a single line of code.

%R y = c(2,3,4,5) # This is an R command... robjects.r.mean(robjects.r.y) # ... but this is a Python command!
R object with classes: ('numeric',) mapped to: <FloatVector - Python:0x7fad05901128 / R:0x6fdd748> [3.500000]

Within Sage, there is yet another option: Sage's own interface to R. If your kernel is not currently "SageMath 8.1", switch it now.

r # This is a predefined object in Sage!
R Interpreter
r?

If you assign r as a variable name, you will of course lose the predefined value. But there is another way to get a hold of it...

sage.interfaces.r.r is r
True
r('x <- c(2,3,4)') r('x')
[1] 2 3 4
x = r('c(2,3,4)'); x
[1] 2 3 4
x = r([2,3,4]); x # Sage's R interface is smarter about conversions.
[1] 2 3 4
r.mean(x)
[1] 3
x.mean() # Sage's R objects have methods!
[1] 3
x.sd()
[1] 1
s = r('sunspots')
s
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 1749 58.0 62.6 70.0 55.7 85.0 83.5 94.8 66.3 75.9 75.5 158.6 85.2 1750 73.3 75.9 89.2 88.3 90.0 100.0 85.4 103.0 91.2 65.7 63.3 75.4 1751 70.0 43.5 45.3 56.4 60.7 50.7 66.3 59.8 23.5 23.2 28.5 44.0 1752 35.0 50.0 71.0 59.3 59.7 39.6 78.4 29.3 27.1 46.6 37.6 40.0 1753 44.0 32.0 45.7 38.0 36.0 31.7 22.2 39.0 28.0 25.0 20.0 6.7 1754 0.0 3.0 1.7 13.7 20.7 26.7 18.8 12.3 8.2 24.1 13.2 4.2 1755 10.2 11.2 6.8 6.5 0.0 0.0 8.6 3.2 17.8 23.7 6.8 20.0 1756 12.5 7.1 5.4 9.4 12.5 12.9 3.6 6.4 11.8 14.3 17.0 9.4 1757 14.1 21.2 26.2 30.0 38.1 12.8 25.0 51.3 39.7 32.5 64.7 33.5 1758 37.6 52.0 49.0 72.3 46.4 45.0 44.0 38.7 62.5 37.7 43.0 43.0 1759 48.3 44.0 46.8 47.0 49.0 50.0 51.0 71.3 77.2 59.7 46.3 57.0 1760 67.3 59.5 74.7 58.3 72.0 48.3 66.0 75.6 61.3 50.6 59.7 61.0 1761 70.0 91.0 80.7 71.7 107.2 99.3 94.1 91.1 100.7 88.7 89.7 46.0 1762 43.8 72.8 45.7 60.2 39.9 77.1 33.8 67.7 68.5 69.3 77.8 77.2 1763 56.5 31.9 34.2 32.9 32.7 35.8 54.2 26.5 68.1 46.3 60.9 61.4 1764 59.7 59.7 40.2 34.4 44.3 30.0 30.0 30.0 28.2 28.0 26.0 25.7 1765 24.0 26.0 25.0 22.0 20.2 20.0 27.0 29.7 16.0 14.0 14.0 13.0 1766 12.0 11.0 36.6 6.0 26.8 3.0 3.3 4.0 4.3 5.0 5.7 19.2 1767 27.4 30.0 43.0 32.9 29.8 33.3 21.9 40.8 42.7 44.1 54.7 53.3 1768 53.5 66.1 46.3 42.7 77.7 77.4 52.6 66.8 74.8 77.8 90.6 111.8 1769 73.9 64.2 64.3 96.7 73.6 94.4 118.6 120.3 148.8 158.2 148.1 112.0 1770 104.0 142.5 80.1 51.0 70.1 83.3 109.8 126.3 104.4 103.6 132.2 102.3 1771 36.0 46.2 46.7 64.9 152.7 119.5 67.7 58.5 101.4 90.0 99.7 95.7 1772 100.9 90.8 31.1 92.2 38.0 57.0 77.3 56.2 50.5 78.6 61.3 64.0 1773 54.6 29.0 51.2 32.9 41.1 28.4 27.7 12.7 29.3 26.3 40.9 43.2 1774 46.8 65.4 55.7 43.8 51.3 28.5 17.5 6.6 7.9 14.0 17.7 12.2 1775 4.4 0.0 11.6 11.2 3.9 12.3 1.0 7.9 3.2 5.6 15.1 7.9 1776 21.7 11.6 6.3 21.8 11.2 19.0 1.0 24.2 16.0 30.0 35.0 40.0 1777 45.0 36.5 39.0 95.5 80.3 80.7 95.0 112.0 116.2 106.5 146.0 157.3 1778 177.3 109.3 134.0 145.0 238.9 171.6 153.0 140.0 171.7 156.3 150.3 105.0 1779 114.7 165.7 118.0 145.0 140.0 113.7 143.0 112.0 111.0 124.0 114.0 110.0 1780 70.0 98.0 98.0 95.0 107.2 88.0 86.0 86.0 93.7 77.0 60.0 58.7 1781 98.7 74.7 53.0 68.3 104.7 97.7 73.5 66.0 51.0 27.3 67.0 35.2 1782 54.0 37.5 37.0 41.0 54.3 38.0 37.0 44.0 34.0 23.2 31.5 30.0 1783 28.0 38.7 26.7 28.3 23.0 25.2 32.2 20.0 18.0 8.0 15.0 10.5 1784 13.0 8.0 11.0 10.0 6.0 9.0 6.0 10.0 10.0 8.0 17.0 14.0 1785 6.5 8.0 9.0 15.7 20.7 26.3 36.3 20.0 32.0 47.2 40.2 27.3 1786 37.2 47.6 47.7 85.4 92.3 59.0 83.0 89.7 111.5 112.3 116.0 112.7 1787 134.7 106.0 87.4 127.2 134.8 99.2 128.0 137.2 157.3 157.0 141.5 174.0 1788 138.0 129.2 143.3 108.5 113.0 154.2 141.5 136.0 141.0 142.0 94.7 129.5 1789 114.0 125.3 120.0 123.3 123.5 120.0 117.0 103.0 112.0 89.7 134.0 135.5 1790 103.0 127.5 96.3 94.0 93.0 91.0 69.3 87.0 77.3 84.3 82.0 74.0 1791 72.7 62.0 74.0 77.2 73.7 64.2 71.0 43.0 66.5 61.7 67.0 66.0 1792 58.0 64.0 63.0 75.7 62.0 61.0 45.8 60.0 59.0 59.0 57.0 56.0 1793 56.0 55.0 55.5 53.0 52.3 51.0 50.0 29.3 24.0 47.0 44.0 45.7 1794 45.0 44.0 38.0 28.4 55.7 41.5 41.0 40.0 11.1 28.5 67.4 51.4 1795 21.4 39.9 12.6 18.6 31.0 17.1 12.9 25.7 13.5 19.5 25.0 18.0 1796 22.0 23.8 15.7 31.7 21.0 6.7 26.9 1.5 18.4 11.0 8.4 5.1 1797 14.4 4.2 4.0 4.0 7.3 11.1 4.3 6.0 5.7 6.9 5.8 3.0 1798 2.0 4.0 12.4 1.1 0.0 0.0 0.0 3.0 2.4 1.5 12.5 9.9 1799 1.6 12.6 21.7 8.4 8.2 10.6 2.1 0.0 0.0 4.6 2.7 8.6 1800 6.9 9.3 13.9 0.0 5.0 23.7 21.0 19.5 11.5 12.3 10.5 40.1 1801 27.0 29.0 30.0 31.0 32.0 31.2 35.0 38.7 33.5 32.6 39.8 48.2 1802 47.8 47.0 40.8 42.0 44.0 46.0 48.0 50.0 51.8 38.5 34.5 50.0 1803 50.0 50.8 29.5 25.0 44.3 36.0 48.3 34.1 45.3 54.3 51.0 48.0 1804 45.3 48.3 48.0 50.6 33.4 34.8 29.8 43.1 53.0 62.3 61.0 60.0 1805 61.0 44.1 51.4 37.5 39.0 40.5 37.6 42.7 44.4 29.4 41.0 38.3 1806 39.0 29.6 32.7 27.7 26.4 25.6 30.0 26.3 24.0 27.0 25.0 24.0 1807 12.0 12.2 9.6 23.8 10.0 12.0 12.7 12.0 5.7 8.0 2.6 0.0 1808 0.0 4.5 0.0 12.3 13.5 13.5 6.7 8.0 11.7 4.7 10.5 12.3 1809 7.2 9.2 0.9 2.5 2.0 7.7 0.3 0.2 0.4 0.0 0.0 0.0 1810 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1811 0.0 0.0 0.0 0.0 0.0 0.0 6.6 0.0 2.4 6.1 0.8 1.1 1812 11.3 1.9 0.7 0.0 1.0 1.3 0.5 15.6 5.2 3.9 7.9 10.1 1813 0.0 10.3 1.9 16.6 5.5 11.2 18.3 8.4 15.3 27.8 16.7 14.3 1814 22.2 12.0 5.7 23.8 5.8 14.9 18.5 2.3 8.1 19.3 14.5 20.1 1815 19.2 32.2 26.2 31.6 9.8 55.9 35.5 47.2 31.5 33.5 37.2 65.0 1816 26.3 68.8 73.7 58.8 44.3 43.6 38.8 23.2 47.8 56.4 38.1 29.9 1817 36.4 57.9 96.2 26.4 21.2 40.0 50.0 45.0 36.7 25.6 28.9 28.4 1818 34.9 22.4 25.4 34.5 53.1 36.4 28.0 31.5 26.1 31.7 10.9 25.8 1819 32.5 20.7 3.7 20.2 19.6 35.0 31.4 26.1 14.9 27.5 25.1 30.6 1820 19.2 26.6 4.5 19.4 29.3 10.8 20.6 25.9 5.2 9.0 7.9 9.7 1821 21.5 4.3 5.7 9.2 1.7 1.8 2.5 4.8 4.4 18.8 4.4 0.0 1822 0.0 0.9 16.1 13.5 1.5 5.6 7.9 2.1 0.0 0.4 0.0 0.0 1823 0.0 0.0 0.6 0.0 0.0 0.0 0.5 0.0 0.0 0.0 0.0 20.4 1824 21.6 10.8 0.0 19.4 2.8 0.0 0.0 1.4 20.5 25.2 0.0 0.8 1825 5.0 15.5 22.4 3.8 15.4 15.4 30.9 25.4 15.7 15.6 11.7 22.0 1826 17.7 18.2 36.7 24.0 32.4 37.1 52.5 39.6 18.9 50.6 39.5 68.1 1827 34.6 47.4 57.8 46.0 56.3 56.7 42.9 53.7 49.6 57.2 48.2 46.1 1828 52.8 64.4 65.0 61.1 89.1 98.0 54.3 76.4 50.4 54.7 57.0 46.6 1829 43.0 49.4 72.3 95.0 67.5 73.9 90.8 78.3 52.8 57.2 67.6 56.5 1830 52.2 72.1 84.6 107.1 66.3 65.1 43.9 50.7 62.1 84.4 81.2 82.1 1831 47.5 50.1 93.4 54.6 38.1 33.4 45.2 54.9 37.9 46.2 43.5 28.9 1832 30.9 55.5 55.1 26.9 41.3 26.7 13.9 8.9 8.2 21.1 14.3 27.5 1833 11.3 14.9 11.8 2.8 12.9 1.0 7.0 5.7 11.6 7.5 5.9 9.9 1834 4.9 18.1 3.9 1.4 8.8 7.8 8.7 4.0 11.5 24.8 30.5 34.5 1835 7.5 24.5 19.7 61.5 43.6 33.2 59.8 59.0 100.8 95.2 100.0 77.5 1836 88.6 107.6 98.1 142.9 111.4 124.7 116.7 107.8 95.1 137.4 120.9 206.2 1837 188.0 175.6 134.6 138.2 111.3 158.0 162.8 134.0 96.3 123.7 107.0 129.8 1838 144.9 84.8 140.8 126.6 137.6 94.5 108.2 78.8 73.6 90.8 77.4 79.8 1839 107.6 102.5 77.7 61.8 53.8 54.6 84.7 131.2 132.7 90.8 68.8 63.6 1840 81.2 87.7 55.5 65.9 69.2 48.5 60.7 57.8 74.0 49.8 54.3 53.7 1841 24.0 29.9 29.7 42.6 67.4 55.7 30.8 39.3 35.1 28.5 19.8 38.8 1842 20.4 22.1 21.7 26.9 24.9 20.5 12.6 26.5 18.5 38.1 40.5 17.6 1843 13.3 3.5 8.3 8.8 21.1 10.5 9.5 11.8 4.2 5.3 19.1 12.7 1844 9.4 14.7 13.6 20.8 12.0 3.7 21.2 23.9 6.9 21.5 10.7 21.6 1845 25.7 43.6 43.3 56.9 47.8 31.1 30.6 32.3 29.6 40.7 39.4 59.7 1846 38.7 51.0 63.9 69.2 59.9 65.1 46.5 54.8 107.1 55.9 60.4 65.5 1847 62.6 44.9 85.7 44.7 75.4 85.3 52.2 140.6 161.2 180.4 138.9 109.6 1848 159.1 111.8 108.9 107.1 102.2 123.8 139.2 132.5 100.3 132.4 114.6 159.9 1849 156.7 131.7 96.5 102.5 80.6 81.2 78.0 61.3 93.7 71.5 99.7 97.0 1850 78.0 89.4 82.6 44.1 61.6 70.0 39.1 61.6 86.2 71.0 54.8 60.0 1851 75.5 105.4 64.6 56.5 62.6 63.2 36.1 57.4 67.9 62.5 50.9 71.4 1852 68.4 67.5 61.2 65.4 54.9 46.9 42.0 39.7 37.5 67.3 54.3 45.4 1853 41.1 42.9 37.7 47.6 34.7 40.0 45.9 50.4 33.5 42.3 28.8 23.4 1854 15.4 20.0 20.7 26.4 24.0 21.1 18.7 15.8 22.4 12.7 28.2 21.4 1855 12.3 11.4 17.4 4.4 9.1 5.3 0.4 3.1 0.0 9.7 4.3 3.1 1856 0.5 4.9 0.4 6.5 0.0 5.0 4.6 5.9 4.4 4.5 7.7 7.2 1857 13.7 7.4 5.2 11.1 29.2 16.0 22.2 16.9 42.4 40.6 31.4 37.2 1858 39.0 34.9 57.5 38.3 41.4 44.5 56.7 55.3 80.1 91.2 51.9 66.9 1859 83.7 87.6 90.3 85.7 91.0 87.1 95.2 106.8 105.8 114.6 97.2 81.0 1860 81.5 88.0 98.9 71.4 107.1 108.6 116.7 100.3 92.2 90.1 97.9 95.6 1861 62.3 77.8 101.0 98.5 56.8 87.8 78.0 82.5 79.9 67.2 53.7 80.5 1862 63.1 64.5 43.6 53.7 64.4 84.0 73.4 62.5 66.6 42.0 50.6 40.9 1863 48.3 56.7 66.4 40.6 53.8 40.8 32.7 48.1 22.0 39.9 37.7 41.2 1864 57.7 47.1 66.3 35.8 40.6 57.8 54.7 54.8 28.5 33.9 57.6 28.6 1865 48.7 39.3 39.5 29.4 34.5 33.6 26.8 37.8 21.6 17.1 24.6 12.8 1866 31.6 38.4 24.6 17.6 12.9 16.5 9.3 12.7 7.3 14.1 9.0 1.5 1867 0.0 0.7 9.2 5.1 2.9 1.5 5.0 4.9 9.8 13.5 9.3 25.2 1868 15.6 15.8 26.5 36.6 26.7 31.1 28.6 34.4 43.8 61.7 59.1 67.6 1869 60.9 59.3 52.7 41.0 104.0 108.4 59.2 79.6 80.6 59.4 77.4 104.3 1870 77.3 114.9 159.4 160.0 176.0 135.6 132.4 153.8 136.0 146.4 147.5 130.0 1871 88.3 125.3 143.2 162.4 145.5 91.7 103.0 110.0 80.3 89.0 105.4 90.3 1872 79.5 120.1 88.4 102.1 107.6 109.9 105.5 92.9 114.6 103.5 112.0 83.9 1873 86.7 107.0 98.3 76.2 47.9 44.8 66.9 68.2 47.5 47.4 55.4 49.2 1874 60.8 64.2 46.4 32.0 44.6 38.2 67.8 61.3 28.0 34.3 28.9 29.3 1875 14.6 22.2 33.8 29.1 11.5 23.9 12.5 14.6 2.4 12.7 17.7 9.9 1876 14.3 15.0 31.2 2.3 5.1 1.6 15.2 8.8 9.9 14.3 9.9 8.2 1877 24.4 8.7 11.7 15.8 21.2 13.4 5.9 6.3 16.4 6.7 14.5 2.3 1878 3.3 6.0 7.8 0.1 5.8 6.4 0.1 0.0 5.3 1.1 4.1 0.5 1879 0.8 0.6 0.0 6.2 2.4 4.8 7.5 10.7 6.1 12.3 12.9 7.2 1880 24.0 27.5 19.5 19.3 23.5 34.1 21.9 48.1 66.0 43.0 30.7 29.6 1881 36.4 53.2 51.5 51.7 43.5 60.5 76.9 58.0 53.2 64.0 54.8 47.3 1882 45.0 69.3 67.5 95.8 64.1 45.2 45.4 40.4 57.7 59.2 84.4 41.8 1883 60.6 46.9 42.8 82.1 32.1 76.5 80.6 46.0 52.6 83.8 84.5 75.9 1884 91.5 86.9 86.8 76.1 66.5 51.2 53.1 55.8 61.9 47.8 36.6 47.2 1885 42.8 71.8 49.8 55.0 73.0 83.7 66.5 50.0 39.6 38.7 33.3 21.7 1886 29.9 25.9 57.3 43.7 30.7 27.1 30.3 16.9 21.4 8.6 0.3 12.4 1887 10.3 13.2 4.2 6.9 20.0 15.7 23.3 21.4 7.4 6.6 6.9 20.7 1888 12.7 7.1 7.8 5.1 7.0 7.1 3.1 2.8 8.8 2.1 10.7 6.7 1889 0.8 8.5 7.0 4.3 2.4 6.4 9.7 20.6 6.5 2.1 0.2 6.7 1890 5.3 0.6 5.1 1.6 4.8 1.3 11.6 8.5 17.2 11.2 9.6 7.8 1891 13.5 22.2 10.4 20.5 41.1 48.3 58.8 33.2 53.8 51.5 41.9 32.3 1892 69.1 75.6 49.9 69.6 79.6 76.3 76.8 101.4 62.8 70.5 65.4 78.6 1893 75.0 73.0 65.7 88.1 84.7 88.2 88.8 129.2 77.9 79.7 75.1 93.8 1894 83.2 84.6 52.3 81.6 101.2 98.9 106.0 70.3 65.9 75.5 56.6 60.0 1895 63.3 67.2 61.0 76.9 67.5 71.5 47.8 68.9 57.7 67.9 47.2 70.7 1896 29.0 57.4 52.0 43.8 27.7 49.0 45.0 27.2 61.3 28.4 38.0 42.6 1897 40.6 29.4 29.1 31.0 20.0 11.3 27.6 21.8 48.1 14.3 8.4 33.3 1898 30.2 36.4 38.3 14.5 25.8 22.3 9.0 31.4 34.8 34.4 30.9 12.6 1899 19.5 9.2 18.1 14.2 7.7 20.5 13.5 2.9 8.4 13.0 7.8 10.5 1900 9.4 13.6 8.6 16.0 15.2 12.1 8.3 4.3 8.3 12.9 4.5 0.3 1901 0.2 2.4 4.5 0.0 10.2 5.8 0.7 1.0 0.6 3.7 3.8 0.0 1902 5.2 0.0 12.4 0.0 2.8 1.4 0.9 2.3 7.6 16.3 10.3 1.1 1903 8.3 17.0 13.5 26.1 14.6 16.3 27.9 28.8 11.1 38.9 44.5 45.6 1904 31.6 24.5 37.2 43.0 39.5 41.9 50.6 58.2 30.1 54.2 38.0 54.6 1905 54.8 85.8 56.5 39.3 48.0 49.0 73.0 58.8 55.0 78.7 107.2 55.5 1906 45.5 31.3 64.5 55.3 57.7 63.2 103.6 47.7 56.1 17.8 38.9 64.7 1907 76.4 108.2 60.7 52.6 42.9 40.4 49.7 54.3 85.0 65.4 61.5 47.3 1908 39.2 33.9 28.7 57.6 40.8 48.1 39.5 90.5 86.9 32.3 45.5 39.5 1909 56.7 46.6 66.3 32.3 36.0 22.6 35.8 23.1 38.8 58.4 55.8 54.2 1910 26.4 31.5 21.4 8.4 22.2 12.3 14.1 11.5 26.2 38.3 4.9 5.8 1911 3.4 9.0 7.8 16.5 9.0 2.2 3.5 4.0 4.0 2.6 4.2 2.2 1912 0.3 0.0 4.9 4.5 4.4 4.1 3.0 0.3 9.5 4.6 1.1 6.4 1913 2.3 2.9 0.5 0.9 0.0 0.0 1.7 0.2 1.2 3.1 0.7 3.8 1914 2.8 2.6 3.1 17.3 5.2 11.4 5.4 7.7 12.7 8.2 16.4 22.3 1915 23.0 42.3 38.8 41.3 33.0 68.8 71.6 69.6 49.5 53.5 42.5 34.5 1916 45.3 55.4 67.0 71.8 74.5 67.7 53.5 35.2 45.1 50.7 65.6 53.0 1917 74.7 71.9 94.8 74.7 114.1 114.9 119.8 154.5 129.4 72.2 96.4 129.3 1918 96.0 65.3 72.2 80.5 76.7 59.4 107.6 101.7 79.9 85.0 83.4 59.2 1919 48.1 79.5 66.5 51.8 88.1 111.2 64.7 69.0 54.7 52.8 42.0 34.9 1920 51.1 53.9 70.2 14.8 33.3 38.7 27.5 19.2 36.3 49.6 27.2 29.9 1921 31.5 28.3 26.7 32.4 22.2 33.7 41.9 22.8 17.8 18.2 17.8 20.3 1922 11.8 26.4 54.7 11.0 8.0 5.8 10.9 6.5 4.7 6.2 7.4 17.5 1923 4.5 1.5 3.3 6.1 3.2 9.1 3.5 0.5 13.2 11.6 10.0 2.8 1924 0.5 5.1 1.8 11.3 20.8 24.0 28.1 19.3 25.1 25.6 22.5 16.5 1925 5.5 23.2 18.0 31.7 42.8 47.5 38.5 37.9 60.2 69.2 58.6 98.6 1926 71.8 70.0 62.5 38.5 64.3 73.5 52.3 61.6 60.8 71.5 60.5 79.4 1927 81.6 93.0 69.6 93.5 79.1 59.1 54.9 53.8 68.4 63.1 67.2 45.2 1928 83.5 73.5 85.4 80.6 76.9 91.4 98.0 83.8 89.7 61.4 50.3 59.0 1929 68.9 64.1 50.2 52.8 58.2 71.9 70.2 65.8 34.4 54.0 81.1 108.0 1930 65.3 49.2 35.0 38.2 36.8 28.8 21.9 24.9 32.1 34.4 35.6 25.8 1931 14.6 43.1 30.0 31.2 24.6 15.3 17.4 13.0 19.0 10.0 18.7 17.8 1932 12.1 10.6 11.2 11.2 17.9 22.2 9.6 6.8 4.0 8.9 8.2 11.0 1933 12.3 22.2 10.1 2.9 3.2 5.2 2.8 0.2 5.1 3.0 0.6 0.3 1934 3.4 7.8 4.3 11.3 19.7 6.7 9.3 8.3 4.0 5.7 8.7 15.4 1935 18.9 20.5 23.1 12.2 27.3 45.7 33.9 30.1 42.1 53.2 64.2 61.5 1936 62.8 74.3 77.1 74.9 54.6 70.0 52.3 87.0 76.0 89.0 115.4 123.4 1937 132.5 128.5 83.9 109.3 116.7 130.3 145.1 137.7 100.7 124.9 74.4 88.8 1938 98.4 119.2 86.5 101.0 127.4 97.5 165.3 115.7 89.6 99.1 122.2 92.7 1939 80.3 77.4 64.6 109.1 118.3 101.0 97.6 105.8 112.6 88.1 68.1 42.1 1940 50.5 59.4 83.3 60.7 54.4 83.9 67.5 105.5 66.5 55.0 58.4 68.3 1941 45.6 44.5 46.4 32.8 29.5 59.8 66.9 60.0 65.9 46.3 38.3 33.7 1942 35.6 52.8 54.2 60.7 25.0 11.4 17.7 20.2 17.2 19.2 30.7 22.5 1943 12.4 28.9 27.4 26.1 14.1 7.6 13.2 19.4 10.0 7.8 10.2 18.8 1944 3.7 0.5 11.0 0.3 2.5 5.0 5.0 16.7 14.3 16.9 10.8 28.4 1945 18.5 12.7 21.5 32.0 30.6 36.2 42.6 25.9 34.9 68.8 46.0 27.4 1946 47.6 86.2 76.6 75.7 84.9 73.5 116.2 107.2 94.4 102.3 123.8 121.7 1947 115.7 113.4 129.8 149.8 201.3 163.9 157.9 188.8 169.4 163.6 128.0 116.5 1948 108.5 86.1 94.8 189.7 174.0 167.8 142.2 157.9 143.3 136.3 95.8 138.0 1949 119.1 182.3 157.5 147.0 106.2 121.7 125.8 123.8 145.3 131.6 143.5 117.6 1950 101.6 94.8 109.7 113.4 106.2 83.6 91.0 85.2 51.3 61.4 54.8 54.1 1951 59.9 59.9 59.9 92.9 108.5 100.6 61.5 61.0 83.1 51.6 52.4 45.8 1952 40.7 22.7 22.0 29.1 23.4 36.4 39.3 54.9 28.2 23.8 22.1 34.3 1953 26.5 3.9 10.0 27.8 12.5 21.8 8.6 23.5 19.3 8.2 1.6 2.5 1954 0.2 0.5 10.9 1.8 0.8 0.2 4.8 8.4 1.5 7.0 9.2 7.6 1955 23.1 20.8 4.9 11.3 28.9 31.7 26.7 40.7 42.7 58.5 89.2 76.9 1956 73.6 124.0 118.4 110.7 136.6 116.6 129.1 169.6 173.2 155.3 201.3 192.1 1957 165.0 130.2 157.4 175.2 164.6 200.7 187.2 158.0 235.8 253.8 210.9 239.4 1958 202.5 164.9 190.7 196.0 175.3 171.5 191.4 200.2 201.2 181.5 152.3 187.6 1959 217.4 143.1 185.7 163.3 172.0 168.7 149.6 199.6 145.2 111.4 124.0 125.0 1960 146.3 106.0 102.2 122.0 119.6 110.2 121.7 134.1 127.2 82.8 89.6 85.6 1961 57.9 46.1 53.0 61.4 51.0 77.4 70.2 55.9 63.6 37.7 32.6 40.0 1962 38.7 50.3 45.6 46.4 43.7 42.0 21.8 21.8 51.3 39.5 26.9 23.2 1963 19.8 24.4 17.1 29.3 43.0 35.9 19.6 33.2 38.8 35.3 23.4 14.9 1964 15.3 17.7 16.5 8.6 9.5 9.1 3.1 9.3 4.7 6.1 7.4 15.1 1965 17.5 14.2 11.7 6.8 24.1 15.9 11.9 8.9 16.8 20.1 15.8 17.0 1966 28.2 24.4 25.3 48.7 45.3 47.7 56.7 51.2 50.2 57.2 57.2 70.4 1967 110.9 93.6 111.8 69.5 86.5 67.3 91.5 107.2 76.8 88.2 94.3 126.4 1968 121.8 111.9 92.2 81.2 127.2 110.3 96.1 109.3 117.2 107.7 86.0 109.8 1969 104.4 120.5 135.8 106.8 120.0 106.0 96.8 98.0 91.3 95.7 93.5 97.9 1970 111.5 127.8 102.9 109.5 127.5 106.8 112.5 93.0 99.5 86.6 95.2 83.5 1971 91.3 79.0 60.7 71.8 57.5 49.8 81.0 61.4 50.2 51.7 63.2 82.2 1972 61.5 88.4 80.1 63.2 80.5 88.0 76.5 76.8 64.0 61.3 41.6 45.3 1973 43.4 42.9 46.0 57.7 42.4 39.5 23.1 25.6 59.3 30.7 23.9 23.3 1974 27.6 26.0 21.3 40.3 39.5 36.0 55.8 33.6 40.2 47.1 25.0 20.5 1975 18.9 11.5 11.5 5.1 9.0 11.4 28.2 39.7 13.9 9.1 19.4 7.8 1976 8.1 4.3 21.9 18.8 12.4 12.2 1.9 16.4 13.5 20.6 5.2 15.3 1977 16.4 23.1 8.7 12.9 18.6 38.5 21.4 30.1 44.0 43.8 29.1 43.2 1978 51.9 93.6 76.5 99.7 82.7 95.1 70.4 58.1 138.2 125.1 97.9 122.7 1979 166.6 137.5 138.0 101.5 134.4 149.5 159.4 142.2 188.4 186.2 183.3 176.3 1980 159.6 155.0 126.2 164.1 179.9 157.3 136.3 135.4 155.0 164.7 147.9 174.4 1981 114.0 141.3 135.5 156.4 127.5 90.0 143.8 158.7 167.3 162.4 137.5 150.1 1982 111.2 163.6 153.8 122.0 82.2 110.4 106.1 107.6 118.8 94.7 98.1 127.0 1983 84.3 51.0 66.5 80.7 99.2 91.1 82.2 71.8 50.3 55.8 33.3 33.4
len(s)
2820
r.plot(s, main = "sunspots data", xlab = "Year", ylab = "Monthly sunspot numbers") # Will this work?
null device 1

Did that actually fail? Check your file directory.

%%R write.csv(sunspots, file = "sunspots.csv")
import pandas as pd
sunspots = pd.read_csv("sunspots.csv")
type(sunspots)
<class 'pandas.core.frame.DataFrame'>
sunspots.describe()
Unnamed: 0 x
count 2820.000000 2820.000000
mean 1410.500000 51.265957
std 814.208204 43.448971
min 1.000000 0.000000
25% 705.750000 15.700000
50% 1410.500000 42.000000
75% 2115.250000 74.925000
max 2820.000000 253.800000
sunspots.iloc(1)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-32-650a4c59e077> in <module>() ----> 1 sunspots.iloc(Integer(1)) /ext/sage/sage-8.1/local/lib/python2.7/site-packages/pandas/core/indexing.pyc in __call__(self, axis) 101 102 if axis is not None: --> 103 axis = self.obj._get_axis_number(axis) 104 new_self.axis = axis 105 return new_self /ext/sage/sage-8.1/local/lib/python2.7/site-packages/pandas/core/generic.pyc in _get_axis_number(self, axis) 355 pass 356 raise ValueError('No axis named {0} for object type {1}' --> 357 .format(axis, type(self))) 358 359 def _get_axis_name(self, axis): ValueError: No axis named 1 for object type <class 'pandas.core.frame.DataFrame'>
sunspots.plot(y='x')