This repository has been archived by the owner on Jan 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
r_intro_datatypes.R
82 lines (67 loc) · 2.57 KB
/
r_intro_datatypes.R
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
food_drink <- c("coffee", "tea", "cake", "sugar")
subset(food_drink, c(TRUE, FALSE, TRUE, TRUE))
food_drink[c(TRUE, FALSE, TRUE, TRUE)]
food_drink[c(2, 4)]
food_drink[-2]
food_drink[1:2]
food_drink[food_drink == "tea"]
food_drink[stringr::str_length(food_drink) > 3]
c("coffee", "tea") == "tea"
# type ! = for UNEQUAL operator
# type == for EQUAL operator
#===== Matrices
m <- matrix(data = c(1,2,3,4), nrow = 2)
a <- array(data = 1:8, dim = c(2, 2, 2))
a[ ,1 ,]
#==== list
# rmarkdown::render_site(input = ".") # this is a normal function, which only accepts SOME named arguments
l <- list(number = c(12,13), fruit = "apple", nested_list = list(first = 1/3, second = 2/3))
# list is a special kind of function that also allows arbitrary numbers of arbitrarily named arguments (Non-standard evaluation, NSE)
# sum(l[1]) # this does not work because it just gives the "box", which can't be summed
sum(l[[1]])
l["fruit"] # this gives the box
l[["fruit"]] # this gives content of the box
l$fruit # same as line above, just with syntatic sugar
# ========= names
vec <- c(one = 1, five = 5) # now a named vector
vec[1] # can still subset on indeces
vec["five"] # can also subset on the name
vec[["five"]] # this works as well, but gives same result, because there's no box
l["fruit"] # same syntax as for vectors
# matrices can also have names
m <- matrix(
data = m,
nrow = 2,
dimnames = list(
countries = c("france", "germany"),
fruit = c("grapes", "apples")
)
)
m["france", "apples"]
unnamed_vec <- c(1, 2)
names(unnamed_vec) <- c("one", "two")
# this is how names are actually done behind the scenes
# name_this(object = unnamed_vec, names = c("one", "two")) # line 55 is, in turn, a cleverer version of something like line 56 implemented in C, which is why we can't use/see it directly
# ==== dataframes
# below separate vectors are scary, because the ordering might change and links between same individuals might get confused
# indeces are individuals in the below, but this is not visible or properly stored
df <- data.frame(
# DATA (this gets filled into the ... argument)
shoesize = c(39, 42, 45),
age = c(21, 19, 24),
gender = c("female", "other", "male"),
name = c("Lisa", "Alex", "Peter"),
# OTHER ARGUMENTS
stringsAsFactors = FALSE
# what is a data frame?
# list of vectors each with the same length
# indeces match across rows
# list elements (which become df columns) are vectors
# (later on, columns also be lists or matrices)
)
df
# question: how to find row based on some value in a cell
df
df$name == "Alex"
df[df$name == "Alex", ]
View(df)