library(reticulate) # to use python in RStudio
library(tidyverse) # data wrangling and plotting with R
In this note, R code chunks are in light pink, while python in light blue. I keep it mainly as a study note, but hopefully it might be of interest to fellow R users learning Python, and Python users learning R.
val=2
if (val==1) {
print("doe a dear")
} else if (val==2) {
print("ray a drop of golden sun")
} else {
print("sound of music")
}
## [1] "ray a drop of golden sun"
val=1
if val==1:
print("doe a dear")
elif val==2:
print("ray a drop of of golden sun")
else:
print("sound of music")
## doe a dear
num_val=c(1:4)
for (value in num_val) {
print(value*2)
}
## [1] 2
## [1] 4
## [1] 6
## [1] 8
num_val=[1,2,3,4]
for value in num_val:
print(value*2)
## 2
## 4
## 6
## 8
(For more on loops in python, including for and while loops, please refer to this note)
num_scores = [5,7,2,3,9,5,4,2,0,1,7]
for scores in num_scores:
if scores >5:
print("Top goalscorers")
else:
print("Players")
## Players
## Top goalscorers
## Players
## Players
## Top goalscorers
## Players
## Players
## Players
## Players
## Players
## Top goalscorers
list = [1,2,3,4,5,6]
square_list = [x**2 for x in list]
square_list
## [1, 4, 9, 16, 25, 36]
vector <- c(1:6)
square_vector <- vector^2
square_vector
## [1] 1 4 9 16 25 36
d = {x:x**2 for x in list}
d
## {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36}