-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-frames.R
More file actions
120 lines (91 loc) · 3.08 KB
/
Copy pathdata-frames.R
File metadata and controls
120 lines (91 loc) · 3.08 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
# Data Frames are data displayed in a format as a table.
# Data Frames can have different types of data inside it. However, each column should have the same type of data.
# Use the data.frame() function to create a data frame
# Create a data frame
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Data_Frame # Print the data frame
# SUMMARIZE THE DATA
# Use the summary() function to summarize the data from a Data Frame
summary(Data_Frame)
# ACCESS ITEMS
# We can use single brackets [ ], double brackets [[ ]] or $ to access columns from a data frame
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Data_Frame[1]
Data_Frame[["Training"]]
Data_Frame$Training
# ADD ROWS
# Use the rbind() function to add new rows in a Data Frame
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
# Add a new row
New_row_DF <- rbind(Data_Frame, c("Strength", 110, 110))
# Print the new row
New_row_DF
# ADD COLUMNS
# Use the cbind() function to add new columns in a Data Frame
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
# Add a new column
New_col_DF <- cbind(Data_Frame, Steps = c(1000, 6000, 2000))
# Print the new column
New_col_DF
# REMOVE ROWS AND COLUMNS
# Use the c() function to remove rows and columns in a Data Frame
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
# Remove the first row and column
Data_Frame_New <- Data_Frame[-c(1), -c(1)]
# Print the new data frame
Data_Frame_New
# AMOUNT OF ROWS AND COLUMNS
# Use the dim() function to find the amount of rows and columns in a Data FramE
dim(Data_Frame)
# You can also use the ncol() function to find the number of columns and nrow() to find the number of rows
ncol(Data_Frame)
nrow(Data_Frame)
# DATA FRAME LENGTH
# Use the length() function to find the number of columns in a Data Frame (similar to ncol())
length(Data_Frame)
# COMBINING DATA FRAME
# Use the rbind() function to combine two or more data frames in R vertically
Data_Frame1 <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Data_Frame2 <- data.frame (
Training = c("Stamina", "Stamina", "Strength"),
Pulse = c(140, 150, 160),
Duration = c(30, 30, 20)
)
New_Data_Frame <- rbind(Data_Frame1, Data_Frame2)
New_Data_Frame
# And use the cbind() function to combine two or more data frames in R horizontallY
Data_Frame3 <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Data_Frame4 <- data.frame (
Steps = c(3000, 6000, 2000),
Calories = c(300, 400, 300)
)
New_Data_Frame1 <- cbind(Data_Frame3, Data_Frame4)
New_Data_Frame1