Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

DATAFRAMES

Dr.S.Karthigai Selvi
What is Dataframe?
 A data frame is a two-dimensional data structure which can
store data in tabular format.
 Data frames have rows and columns and each column can
be a different vector. And different vectors can be of
different data types.

df <- data.frame( first_col = c(val1, val2, ...),


second_col = c(val1, val2, ...), ... )
 Here,
 first_col - a vector with values val1, val2, ... of same data
type
 second_col - another vector with values val1, val2, ... of
same data type and so on
Example
 There are different ways to extract columns from a
data frame. We can use [ ], [[ ]], or $ to access
specific column of a data frame in R. For example,

# Create a data frame


df <- data.frame ( Name = c(“Arun", “Gobi“, “Amit"),
Age = c(22, 15, 19),
Vote = c(TRUE, FALSE, TRUE) )
print(df)
Example
# Create a data frame
dataframe1 <- data.frame ( • The symbol [ ] return
Name = c("Juan", "Alcaraz", "Simantha"),
Age = c(22, 15, 19),
data as dataframe
Vote = c(TRUE, FALSE, TRUE) • [[ ]] and $ - returns
)
data as vectors.
# pass index number inside [ ]
print(dataframe1[1])

# pass column name inside [[ ]]


print(dataframe1[["Name"]])

# use $ operator and column name


print(dataframe1$Name)
Combine Dataframes
we use the rbind() and the cbind() function to combine
two data frames together.
 rbind() - combines two data frames vertically

 cbind() - combines two data frames horizontally

 Combine Vertically using rbind()

 Combine Horizontally using cbind()


# create a data frame
df1 <- data.frame (
Name = c(“Bhagat", “Nisha"),
Age = c(22, 15))
# create another data frame
df2 <- data.frame (
Name = c(“Yamuna", “Baby"),
Age = c(46, 89))
# combine two data frames vertically
updated <- rbind(df1, df2)
print(updated)
Output
Name Age
[1] Bhagat 22
[2] Nisha 15
[3] Yamuna 46
[4] Baby 89
Length of a Data Frame in R

 length() function to find the number of columns in a


data frame.

# create a data frame


df <- data.frame (
Name = c(“Bhagat", “Nisha"),
Age = c(22, 15))
#print the no.of columns in a data frame
cat(“Total Elements:”, length(df))
Factors
 A Factor is a data structure that is used to work with
categorizable datas.
 Suppose a data field such as marital status may
contain only values from single, married, separated,
divorced, or widowed.
 In such a case, we know the possible values
beforehand and these predefined, distinct values
are called levels of a factor.
Create a factor in R
 In R, we use the factor() function to create a factor.
Once a factor is created, it can only contain
predefined set values called levels.
 The syntax for creating a factor is

factor(vector)
Example
# create a factor
students_gender <- factor(c("male", "female", "male",
"transgender", "female"))
# print the marital_status factor
print(students_gender)
Accessing Elements from a Factor
# create a factor
students_gender <- factor(c("male", "female", "male",
"transgender", "female"))
# access 1st element of students gender
print(students_gender[1])
# access 4th element of students_gender
print(students_gender[4])
Modify Factor Element

 To change a vector element, we can simply reassign


a new value to the specific index.

# create a factor
marital_status <- factor(c("married",
"single", "single", "divorced", "married"))
# print the marital_status factor
marital_status[1] <- "divorced"
print(marital_status[1])

You might also like