-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_DATAFRAME.R
144 lines (114 loc) · 4 KB
/
4_DATAFRAME.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
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
# CREATING DATA FRAME
In all below tasks create your own toy objects.
#Task 1: Write a R program to create an empty data frame.
df = data.frame(Characters=character(),
Ints=integer(),
Doubles=double(),
stringsAsFactors=FALSE
)
print(str(df))
#Task 2: Write a R program to create a data frame from four given vectors.
name = c('Bq', 'Desy', 'Hardianti')
nostudent = c(1245, 3456, 1122)
exam = c(80.5, 85.8, 75.3)
approve = c('no', 'yes', 'yes')
print(name)
print(nostudent)
print(exam)
print(approve)
df = data.frame(name,nostudent,exam,approve)
print(df)
#Task 3: Write a R program to get the structure of a given data frame.
data1 = data.frame(
name = c('Bq', 'Desy', 'Hardianti'),
nostudent = c(1245, 3456, 1122),
exam = c(80.5, 85.8, 75.3),
approve = c('no', 'yes', 'yes')
)
print(str(data1))
#Task 4: Write a R program to get the statistical summary and nature of the data of a given data frame.
data1 = data.frame(
name = c('Bq', 'Desy', 'Hardianti'),
nostudent = c(1245, 3456, 1122),
exam = c(80.5, 85.8, 75.3),
approve = c('no', 'yes', 'yes')
)
print(summary(data1))
#Task 5: Write a R program to extract 3rd and 5th rows with 1st and 3rd columns from a given data frame.
data1 = data.frame(
name = c('Bq', 'Desy', 'Hardianti','Arl','Aku'),
nostudent = c(1245, 3456, 1122, 6677, 7788),
exam = c(80.5, 85.8, 75.3, 90.3, 70.5),
approve = c('no', 'yes', 'yes','yes','no')
)
result =data1[c(3,5),c(1,3)]
print(result)
#Task 6: Write a R program to add a new column in a given data frame.
data1 = data.frame(
name = c('Bq', 'Desy', 'Hardianti','Arl','Aku'),
nostudent = c(1245, 3456, 1122, 6677, 7788),
exam = c(80.5, 85.8, 75.3, 90.3, 70.5),
approve = c('no', 'yes', 'yes','yes','no')
)
data1$city = c("Warsaw","Torun","Krakow","Granks","Lods")
print(data1)
#Task 7: Write a R program to drop column(s) by name from a given data frame.
data1 = data.frame(
name = c('Bq', 'Desy', 'Hardianti','Arl','Aku'),
nostudent = c(1245, 3456, 1122, 6677, 7788),
exam = c(80.5, 85.8, 75.3, 90.3, 70.5),
approve = c('no', 'yes', 'yes','yes','no')
)
data1= subset(data1,select=-c(exam,approve))
print(data1)
#Task 8: Write a R program to sort a given data frame by multiple column(s).
data1 = data.frame(
name = c('Bq', 'Desy', 'Hardianti','Arl','Aku'),
nostudent = c(1245, 3456, 1122, 6677, 7788),
exam = c(80.5, 85.8, 75.3, 90.3, 70.5),
approve = c('no', 'yes', 'yes','yes','no')
)
data1 = data1[with(data1, order(name, exam)), ]
print(data1)
#Task 9: Write a R program to create inner, outer, left, right join(merge) from given two data frames.
n1 = data.frame(result = c(1111, 3456, 1122, 5432, 7788))
n2 = data.frame(result = c(1245, 3456, 1122, 6677, 7788))
#Left outer Join
result1 = merge(n1, n2, by = "result", all.x = TRUE)
print(result1)
#Right outer Join
result1 = merge(n1, n2, by = "result", all.y = TRUE)
print(result1)
#"Outer Join
result = merge(n1, n2, by = "result", all = TRUE)
print(result1)
#Cross Join
result1 = merge(n1, n2, by = NULL)
print(result1)
#Task 10: Write a R program to replace NA values with 3 in a given data frame.
data1 = data.frame(
name = c('Bq', 'Desy', 'Hardianti','Arl','Aku'),
nostudent = c(1245, 3456, 1122, 6677, 7788),
exam = c(80.5, NA, 75.3, 90.3, NA),
approve = c('no', 'yes', 'yes','yes','no')
)
data1
data1[is.na(data1)] = 3
print(data1)
#Task 11: Write a R program to compare two data frames to find the elements in first data frame that are not present in second data frame.
n1 = c(1111, 3456, 1122, 5432, 7788)
n2 = c(1245, 3456, 1122, 6677, 7788)
print(n1)
print(n2)
result = setdiff(n1, n2)
print(result)
#Task 12: Write a R program to count the number of NA values in a data frame column.
data1 = data.frame(
name = c('Bq', 'Desy', 'Hardianti','Arl','Aku'),
nostudent = c(NA, 3456, 1122, NA, 7788),
exam = c(80.5, NA, 75.3, 90.3, NA),
approve = c('no', 'yes', 'yes','yes','no')
)
print(data1)
#The number of NA values in exam column
print(sum(is.na(data1$exam)))