0% found this document useful (0 votes)
9 views

Data Frames

The document discusses data frames in R. It explains how to create, access, subset, edit and add rows and columns to data frames. It provides examples of creating data frames with different data types and manipulating them by subsetting, binding rows and columns and editing values.

Uploaded by

Arnav Vikas Garg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Data Frames

The document discusses data frames in R. It explains how to create, access, subset, edit and add rows and columns to data frames. It provides examples of creating data frames with different data types and manipulating them by subsetting, binding rows and columns and editing values.

Uploaded by

Arnav Vikas Garg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Data frames

• Data frames
– Store tabular data
• Create a data frame
Access contents of data frame
• df[1,]
v1 v2
90 Object oriented Programming
Access contents of data frame
• df[,1]
[1] 90 82 73
Access contents of data frame
• > df[,]
• v1 v2
• 1 90 Object oriented Programming
• 2 82 Python
• 3 73 Java
• df2=data.frame("Name"=c("Sahil","Akhil","Rat
an"),"Age"=c(44,33,22),"salary"=c(50,80,20))
• df2
• df2=data.frame("Name"=c("Sahil","Akhil","Rat
an"),"Age"=c(44,33,22),"salary"=c(50,80,20))
• df2
• Name Age salary
• 1 Sahil 44 50
• 2 Akhil 33 80
• 3 Ratan 22 20
• ndf=subset(df2,Name=="Sahil"|Age>30)
• ndf
• ndf=subset(df2,Name=="Sahil"|Age>30)
• Ndf
• Name Age salary
• 1 Sahil 44 50
• 2 Akhil 33 80
• ndf=subset(df2,Name=="Sahil"&Age>30)
• ndf
• Name Age salary
• 1 Sahil 44 50
Editing data frame
df2[[2]][3]=50
df2
Name Age salary
1 Sahil 44 50
2 Akhil 33 80
3 Ratan 50 20
Editing using Edit() command
• > table=data.frame()
• > table=edit(table)
• > table
• Name Marks
• 1 Neha 100
• 2 Priya 90
• 3 Swapnil 95
Adding rows and columns
• Rows
– rbind
• Cols
– cbind
df2=rbind(df2,data.frame(Name="Kirti",Age=2
0,salary="100"))
df2
df2=rbind(df2,data.frame(Name="Kirti",Age=20,s
alary="100"))
df2
Name Age salary
1 Sahil 44 50
2 Akhil 33 80
3 Ratan 50 20
4 Kirti 20 100
df2=cbind(df2,Address=c("sec-2","sec-3","sec-
4","sec-15"))
df2
df2=cbind(df2,Address=c("sec-2","sec-3","sec-
4","sec-15"))
df2
• Name Age salary Address
• 1 Sahil 44 50 sec-2
• 2 Akhil 33 80 sec-3
• 3 Ratan 50 20 sec-4
• 4 Kirti 20 100 sec-15

You might also like