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
24 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
456 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,700 points
51 views
0 votes
1 answer

How to sort a data frame by columns in R?

You can just use the order function ...READ MORE

answered Apr 10, 2018 in Data Analytics by darklord
• 6,140 points
72 views
0 votes
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,700 points
46 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
• 37,320 points
37 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

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,550 points
3,797 views
+1 vote
1 answer

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

This code gives the total number of ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,550 points
26 views