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,060 points
46 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,550 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 2, 2018 in Data Analytics by Bharani
• 4,550 points
608 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
106 views
0 votes
2 answers

How to sort a data frame by columns in R?

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

answered Aug 21 in Data Analytics by anonymous
• 31,840 points
146 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
129 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 4, 2018 in Data Analytics by Kalgi
• 42,330 points
60 views
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

Filtering R data-frame with multiple conditions

librarry(dplyr) dataset >%> filter((a ==1,b>15 ) | (a==2,b&g ...READ MORE

answered Aug 8 in Data Analytics by anonymous
7,692 views
+1 vote
2 answers