[1] 6.924369
POP77001 Computer Programming for Social Scientists
IRkernel:
install.packages("IRkernel") to install the packageIRkernel::installspec() to initialize R kernel for JupyterTip: When starting working with R in Jupyter run options(jupyter.rich_display = FALSE) command to switch off pretty printing and get the output (albeit less neat) consistent with output in RStudio
IRkernelPackage IRkernel is required to run R in Jupyter Notebook.
More often than not you want to record how analysis was performed.
There are 3 principal ways of distributing R code:
.R file).Rmd/.qmd file).ipynb file)#)._ or * for emphasis (single - italic, double - bold, triple - bold and italic)
*one* becomes one, __two__ - two and ***three*** - three#, ##, ###, #### and so on-, + or *
1. (counter is auto-incremented)[some text here](url_here).md file extension..Rmd file extension (.qmd for Quarto).R Markdown
Rendering
### Title
Some text in *italic* and **bold**
Simple list:
- A
- B
Ordered list:
1. A
1. B
Example, where $Y_i = 5 + X_i + \epsilon$
```{r}
x_i <- 3
epsilon <- rnorm(1)
y_i <- 5 + x_i + epsilon
y_i
```
. in variable names (it works as an object attribute in Python)c, T, list, mean)my_long_vector <- c(
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60
)
long_function_name <- function(a = "a long argument",
b = "another argument",
c = "another long argument") {
# As usual code is indented by two spaces.
}
There are ~14 reserved words in R that cannot be used as names assigned to objects.
break |
NA |
else |
NaN |
FALSE |
next |
for |
NULL |
function |
repeat |
if |
TRUE |
Inf |
while |
Extra
letters (lower-case letters of the Roman alphabet)lettersTip: You can use function which() for determining the indices of vowels
table() provides an easy way of summarizing categorical variables provinces
top_10_settlements Connacht Leinster Munster Ulster
Bangor 0 0 0 1
Belfast 0 0 0 1
Cork 0 0 1 0
Derry 0 0 0 1
Dublin 0 1 0 0
Galway 1 0 0 0
Limerick 0 0 1 0
Lisburn 0 0 0 1
Newtownabbey 0 0 0 1
Waterford 0 0 1 0
table(provinces) is sorted alphabeticallystr()sort() function to sort the vector in a decreasing order (from largest to smallest)provinces vector into a factor with the levels ordered accordinglytable(provinces)letters object under a different nameletters object together in a single character vector