Open In App

Heterogeneous Data in R

Last Updated : 24 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Data structures are a logical way or representing as per requirement. They further help depict this logical view physically in computer memory. In the R language, data structures can be classified into two groups, namely homogeneous and heterogeneous.

  • Homogeneous Data Structures: This type can only store a single type of data inside them(integer, character, etc.),
  • Heterogeneous Data Structures: This type can store more than one type of data at the same time.

Heterogeneous Data Structures

R supports two ways of representing heterogeneous data, namely lists and dataframe. Both structures are discussed in detail below:

1) Lists :

  • Lists are single-dimensional heterogeneous data types.
  • A list can represent more than one data type at a time.
  • We can simply use the list() function to create a list.
  • Lists are similar to vectors, however, vectors are homogeneous and lists are heterogeneous.
  • Another interesting property of lists is that we can store lists inside other lists(like simple recursion). Due to this reason, Lists are also referred to as “Recursive Vectors“.

Example:

R




list_ex = list(Ch="R language", numbers = 5:1, fl=FALSE)
  
print(list_ex)


Output:

$Ch

[1] “R language”

$numbers

[1]  5 4 3 2 1

$fl

[1] FALSE

Example: Recursive vectors

R




list_ex2<-list(list(1,"R language",FALSE), list("Python",2,"Language"),
               list("Hello",FALSE,"World"))
  
str(list_ex2)


Output:

List of 3

$ :List of 3

 ..$ : num 1

 ..$ : chr “R language”

 ..$ : logi FALSE

$ :List of 3

 ..$ : chr “Python” 

 ..$ : num 2

 ..$ : chr “Language”

$ :List of 3

 ..$ : chr “Hello”

 ..$ : logi FALSE

 ..$ : chr “World”          

2) Data Frames:

  • In the R language, a data frame is a two-dimensional heterogeneous table-like structure
  • They are simply lists of vectors that have equal lengths.
  • Data frames make data analysis easier when they are used systematically,
  • In the R language, we use the data.frames() function to create data frames.
  • A data frame in R must follow the following rules :
    • A data frame must have column names and each column must contain equal amount of items,
    • Each row in a data frame must have a unique name,
    • Each entry in a column must have same data type,
    • Different columns can have same or different data types.

Example:

R




employee_id <- c(1:4)
  
employee_name <- c("Abdul", "Anshul", "Vishal", "Riya")
employee_salary <- c("45000", "90000", "25000", "75000")
employee_designation <- c("Software Engineer", "Senior Manager", "Intern","Manager")
  
employee.data <- data.frame(employee_id , employee_name, employee_salary,
                            employee_designation)
  
employee.data


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads