Repository navigation
Expand file tree
/
Copy pathSubsetting in R
More file actions
49 lines (35 loc) · 1.43 KB
/
Copy pathSubsetting in R
File metadata and controls
49 lines (35 loc) · 1.43 KB
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
# Contents
## SUBSETTING A VECTOR
## SUBSETTING A LIST
## SUBSETTING A DATA FRAME
# Note: data.table package provides faster data manipulation in terms of subsetting with simpler code.
# Explained in Data Manipulation with R_part 3.r file
#____________________________________________________________________
## SUBSETTING A VECTOR
a <- c(1, 3, 5, 3, 5.5)
# To get a subset of a (only elements in the range 1<a<4)
a[a > 1 & a < 4]
# To get an index (position) of numbers which are in the range 1<a<4
which(a > 1 & a < 4)
#When using brackets of the form parent[child], single brackets return an object the same class as the parent
#and double brackets returns an object the same class as the child.
## SUBSETTING A LIST
x <- list(a = list(10, 12, 14), b = c(3.14, 2.81))
# [ ] return an object from object x. So x['a'] returns a list. This list has only one member with name of 'a'.
x["a"]
# [[ ]] returns an element of x. [['a']] returns the first element of x, which has a name 'a'. It is a list of length 3,
x[["a"]]
# x$a is equivalent to [['a']].
x$a
## SUBSETTING A DATA FRAME
# From a dataframe, subset where the values of two different columns meet certain criteria
x <- andy # x is assigned the data frame 'andy'
x[x[["Col3"]] > 135 & x[["col4"]] < 18 , ] ## doubts in results
x = list(a = 1:5, b="hi")
y = x["a"]
y
# To create a data frame
n = c(2, 3, 5)
s = c("aa", "bb", "cc")
b = c(TRUE, FALSE, TRUE)
df = data.frame(n, s, b)