I solved 500 R-lang problems to learn R

Updated at: 29 January 2025

For my doctoral studies I decided to learn R lang for data analysis.

1. Print “Hello World”

message <- "hello world"
print(message)

2. Take input name and age from the user and display a message

n <- readline(prompt = "Enter your name: ")
a <- readline(prompt = "Enter your age: ")

print(paste0("Hello, ", n, "!", " You are ", a, " years old."))

paste() combines strings and adds spaces between, paste0() pastes without adding spaces.

3. Find the sum, mean and product of vector

vector = c(1, 5, 3, 6, 7, 15, 85, 22, 189)
sum(vector)
mean(vector)
prod(vector) # multiply all elements of the vector

I didn’t know what a product of a vector is 😮

4. Create numerical, and character vectors

vector = c(1, 5, 3, 6, 7, 15, 85, 22, 189)
vector = c("hello", "it's me", "Mike")

5. Access a specific element in a vector

vector = c("hello", "it's me", "Mike")
vector[3] # prints Mike

6. Calculate the median, skewness and kurtosis of a vector

vector = c(1, 5, 3, 6, 7, 15, 85, 22, 189)
median(vector)
skewness(vector)
kurtosis(vector)

7. Modify an element in a vector

vector[2] = 1 # assigns 1 to the element at position 2 

I found out that unlike in most programming languages, in R the arrays start at position 1, not 0

8. Get length of a vector

length(vector)

9. Combine vectors

combined_vector = c(vector, 2, 4, 123)

10. Sort vectors

sort(combined_vector)
combined_vector # still not sorted
combined_vector = sort(combined_vector)
combined_vector # now it's sorted

I learned that the sort function doesn’t reassign the value, so the product of sorting has to be assigned to a variable.

11. Reverse sort

combined_vector = sort(combined_vector, decreasing = TRUE)
combined_vector = rev(combined_vector)

Two methods doing the same thing.

12. Filter out a part of a vector

filtered_vector <- vector[vector > 5]

I was very surprised by this syntax, very weird for someone coming from JavaScript and PHP world, but I notice the simplicity.

Digging deeper I found that you can also use which() which provides a more familiar syntax:

which(vector > 5)

13. Build a logical index and use it to select values without filtering them out

logical_index <- c(1, 3, 5, 123, 189) > 100
vector[logical_index]
# prints: 123 189

This is something entirely new to me, the logical index is an array of TRUE and FALSE boolean values and when passing it as an index to a vector it prints all values from the vector that are TRUE in the indexer.

14. Add vectors

c(1,12) + c(100,200)
# prints: 101, 212

I learned it’s called vector recycling… but why??? I like the simplicity, in most languages this would require a loop.

You can use other operators using this syntax: * / - %.

15. Create a named vector

named_vector = c(a = 1, b = 2, c = 3)

16. Convert a vector to a list

What is a list? It’s a collection of elements that does not enforce uniform data types.

list = as.list(vector)

17. Create a matrix of vectors

rbind(catNames = c("Puff","Purr","Fluff"), catAge = c(4,5,6), miceCatched = c(7,8,9))

#             [,1]   [,2]   [,3]   
# catNames    "Puff" "Purr" "Fluff"
# catAge      "4"    "5"    "6"
# miceCatched "7"    "8"    "9"

I do not get the reasons why this is not called a table, but lets roll…

Vectors can also be combined vertically:

cbind(catNames = c("Puff","Purr","Fluff"), catAge = c(4,5,6), miceCatched = c(7,8,9))

#      catNames catAge miceCatched
# [1,] "Puff"   "4"    "7"
# [2,] "Purr"   "5"    "8"        
# [3,] "Fluff"  "6"    "9"

18. Create repeating vectors quickly

one_to_ten = seq(1, 10)
all_even_1_to_100 = seq(0, 100, by = 2)

19. Get min and max value

min(seq(1,101))
max(seq(1,101))

20. Find indices of specific values

which(vector > 5)

21. Find unique elements

unique(vector) # removed duplicates

22. Apply a function to each element

sapply(vector, function(x) paste(x, "is a number"))

This will add text to each number saying it’s a number, quite useless 😅.

23. Compare elements of vectors

c(1,2,1,3,1,5,1) == c(1,2,4,1,2,1,5)

You can use all logical operators here.

24. Create a factor from a vector

Ok, what is a factor????

Autor: Michał KuczekMichał Kuczek

Founder of Biiird Studio, UX designer, business philosopher, psychologist, and conflict mediator.