data frame vs matrix

0 votes
I am new to R and am still trying to understand when should a data.frame be used and when should a matrix be used.

I know that both of them are multi-dimensional data types, i want to know when to use which data type.
Apr 27, 2018 in Data Analytics by nirvana
• 3,130 points
1,172 views

1 answer to this question.

0 votes

Yes, both matrix and data.frame are multidimensional data-types and both of them store data in rectangular format.

This is where the difference comes:

A 'matrix' stores only homogenous data while a 'data.frame' stores heterogenous data.

Let me demonstrate this with an example:

Matrix:

temperature<-c(20,22,20,25,20)
humidity<-c(54,50,45,43,50)
climate<-matrix(c(temperature,humidity),nrow=5)
 climate
     [,1] [,2]
[1,]   20   54
[2,]   22   50
[3,]   20   45
[4,]   25   43
[5,]   20   50

This 'climate' matrix takes only numeric vectors and hence it is homogenous.

Data-Frame:

rain<-c(T,F,F,F,T)

rainfall<-data.frame(temp=temperature,humid=humidity,rain=rain)

rainfall
  temp humid  rain
1   20    54  TRUE
2   22    50 FALSE
3   20    45 FALSE
4   25    43 FALSE
5   20    50  TRUE

'rainfall' is a data.frame which consists of heterogenous entries.

answered Apr 27, 2018 by Bharani
• 4,660 points

Related Questions In Data Analytics

0 votes
1 answer

Converting R data-frame to h2o object

All you have to do is run ...READ MORE

answered Apr 3, 2018 in Data Analytics by Bharani
• 4,660 points
2,564 views
0 votes
1 answer
0 votes
1 answer

How to spilt a column of a data frame into multiple columns

it is easily achievable by using "stringr" ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,775 views
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,812 views
+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,661 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 5, 2018 in Data Analytics by Kalgi
• 52,350 points
1,099 views
0 votes
1 answer
0 votes
1 answer
+1 vote
3 answers

Filtering R data-frame with multiple conditions

You can use the 'filter' function from ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
87,150 views
+1 vote
2 answers

Finding number of missing values and removing those missing values from a data-frame

To find number of missing values for ...READ MORE

answered Aug 14, 2019 in Data Analytics by anonymous
1,100 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP