-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSession1b_R-dataframes.Rmd
More file actions
232 lines (166 loc) · 3.65 KB
/
Copy pathSession1b_R-dataframes.Rmd
File metadata and controls
232 lines (166 loc) · 3.65 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
---
title: "R-Dataframes"
output:
html_notebook: default
pdf_document: default
---
# Dataframes
In any form of data analysis, its most important to understand the datasets.
Data frames are form of data structures in tabular format. They are most widely used for data analysis in R.
Data frames,:
- rows : Observations
- columns are vectors of particular data types (such as character, integer,logical).
- different columns can be diff datatype.
- Elements of same column should be same type
Creating a data frame
```{r}
firstName <- c("Vilas","Pallavi", "Archana","Matti", "Dylan","Tain","Fahad", "Ivy","Jason") ## firstName is a Vector
lastName <- c("Menon", "Gaur", "Yadav", "Lam", "Lee","Luquez","Paryani", "Kosater", "Mares")
Gender <- c("male", "female", "female", "male", "male","male","male","female","male")
id_no <- c(20,29,32,15,26,19,30,25,27)
Fav_icecream <- c("Vanilla","Rose","Saffron","Chocolate","Vanilla","Chocolate","Vanilla","Cookies&Cream","Saffron")
Menon_lab <- data.frame(firstName, lastName, FullName=paste(firstName, lastName), Gender, id_no, Fav_icecream) # data.frame is a function, Menon_lab is dataframe
Menon_lab
```
```{r}
getwd()
```
```{r}
Menon_lab
```
```{r}
#summary(Menon_lab)
str(Menon_lab)
```
```{r}
class(Fav_icecream)
```
```{r}
Menon_lab
```
```{r}
#Menon_lab$firstName
table(Menon_lab$Gender)
table(Menon_lab$Gender, Menon_lab$Fav_icecream)
```
```{r}
#save this table
write.csv(Menon_lab,"menon_lab.csv")
```
```{r}
# open and read a csv file
m_lab <- read.csv("menon_lab.csv", row.names = 1)
m_lab
```
```{r}
getwd()
```
```{r}
list.files()
```
```{r}
##Exploring data frame
#Size
dim(Menon_lab) #returns number of rows and the number of columns
nrow(Menon_lab) # number of rows
ncol(Menon_lab) # number of columns
```
```{r}
#Content
head(Menon_lab)
tail(Menon_lab)
colnames(Menon_lab)
rownames(Menon_lab)
#rownames(Menon_lab) <- Menon_lab$firstName
#rownames(Menon_lab)
```
```{r}
str(Menon_lab)
```
```{r}
summary(Menon_lab)
```
####Indexing and subsetting
use [] bracketcs, [row position,column position]
```{r}
Menon_lab
```
```{r}
#Menon_lab[2,]#extract entire row, which is a dataframe with single observation
Menon_lab[2,3] # extract a particular element at row2 column3
```
```{r}
#Extract entire column, which is a vector
#Menon_lab$firstName
#Menon_lab[["firstName"]]
Menon_lab[,"firstName"]
Menon_lab[,"Gender"]
```
```{r}
Menon_lab
```
```{r}
#Extract particular cell
Menon_lab[4,6]
```
```{r}
#extract cell using column name
Menon_lab[4,"firstName"]
```
```{r}
# subsetting
#extract n number of rows
Menon_lab[c(1,2,4,6),]
```
```{r}
##extract some rows and columns
new_set<- Menon_lab[1:5,c("firstName","id_no", "Gender")]
new_set
```
```{r}
Menon_lab
```
```{r}
#extract rows based on some comparison
Menon_lab[Menon_lab$Gender=="female",]
```
```{r}
##make a new table with only female members
Menon_lab[Menon_lab$Gender=="female",]
female_members <- Menon_lab[Menon_lab$Gender=="female",]
female_members
```
```{r}
#show everything except particular column
Menon_lab[,-1]
```
```{r}
#show everything except particular rows
Menon_lab[-c(1,3),]
```
```{r}
Menon_lab
```
###using Grep
```{r}
###using Grep
## grep index values i.e.tell me the name of row numbers where the last name starts with 'L'
grep("^L", Menon_lab$lastName) # ^ is a regular expression also called regex in coding language
```
```{r}
####subsetting with grep
#Menon_lab[]
lastnameL <- Menon_lab[grep("^L",Menon_lab$lastName),]
```
### change the values in data frame
```{r}
# gsub function
Menon_lab$Fav_icecream<- gsub("Vanilla","Mochi",Menon_lab$Fav_icecream )
Menon_lab
```
```{r}
Menon_lab[4,5] <- "50"
```
```{r}
Menon_lab
```