This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path17.Data_Frames_List.r
74 lines (57 loc) · 2.22 KB
/
17.Data_Frames_List.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
# Aim: Write an R Program to access a Data Frame like a List.
# Procedure:
# Write a R program to create a data frame of five students with five fields
# namely Roll number, name, gender, blood group and grade.
# write a code
# 1) to extract specific column namely Grade from a data frame like a list.
# 2) to extract specific column namely roll_number from a data frame like a list.
# 3) to extract specific column namely name from a data frame like a list.
# 4) to drop column (Gender) by name from a given data frame.
# Source Code:
st2_df <- function()
{
roll_number=c("200981A0101","200981A0102","200981A0103","200981A0104","200981A0105")
name=c("Ahmed","John","Rama","Begum","Rani")
gender=c("M","M","M","F","F")
blood=c("A+","B-","O","A-","O")
blood_group=factor(blood)
grade=c(8,8.5,7.5,9,8.5)
stud_df=data.frame(roll_number,name,gender,blood_group,grade)
cat("\n The data frame is\n ")
print(stud_df)
cat("\n Extracting the specific column Grade\n")
print(stud_df$grade)
cat("\n Extracting the specific column roll_number\n")
print(stud_df$roll_number)
cat("\n Extracting the specific column name\n")
print(stud_df$name)
cat("\n Dropping column (Gender) by name from a given data frame \n")
stud_df$gender=NULL
print(stud_df)
}
st2_df()
# OUTPUT:
# The data frame is
# roll_number name gender blood_group grade
# 1 200981A0101 Ahmed M A+ 8.0
# 2 200981A0102 John M B- 8.5
# 3 200981A0103 Rama M O 7.5
# 4 200981A0104 Begum F A- 9.0
# 5 200981A0105 Rani F O 8.5
#
# Extracting the specific column Grade
# [1] 8.0 8.5 7.5 9.0 8.5
#
# Extracting the specific column roll_number
# [1] "200981A0101" "200981A0102" "200981A0103" "200981A0104" "200981A0105"
#
# Extracting the specific column name
# [1] "Ahmed" "John" "Rama" "Begum" "Rani"
#
# Dropping column (Gender) by name from a given data frame
# roll_number name blood_group grade
# 1 200981A0101 Ahmed A+ 8.0
# 2 200981A0102 John B- 8.5
# 3 200981A0103 Rama O 7.5
# 4 200981A0104 Begum A- 9.0
# 5 200981A0105 Rani O 8.5