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 955 views

## 1 answer to this question.

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.

• 4,660 points

## Converting R data-frame to h2o object

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

## How to use a function to repeat a set of procedures on specific set of columns in a data frame?

You can parse the strings to symbols. ...READ MORE

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

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

+1 vote

## How to sort a data frame by columns in R?

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

+1 vote

## 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

## In data frame how to spilt strings into values?

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

+1 vote