Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector vs. Data frame in R

What is the difference between a vector and a data frame in R? Under what circumstances vectors should be converted to data frames?

like image 418
Mehper C. Palavuzlar Avatar asked Feb 17 '10 11:02

Mehper C. Palavuzlar


People also ask

Is a Dataframe a vector?

A data frame is a tabular data structure, consisting of rows and columns and implemented as a list. The columns of a data frame can consist of different data types but each column must be a single data type [like a vector].

What is the difference between a vector and a list in R?

A list holds different data such as Numeric, Character, logical, etc. Vector stores elements of the same type or converts implicitly. Lists are recursive, whereas vector is not. The vector is one-dimensional, whereas the list is a multidimensional object.

How do I convert a data frame to a vector in R?

For the Conversion of dataframe into a vector, we can simply pass the dataframe column name as [[index]]. Approach: We are taking a column in the dataframe and passing it into another variable by the selection method. Selection method can be defined as choosing a column from a data frame using ” [[]]”.

Is vector a data type in R?

R's basic data types are character, numeric, integer, complex, and logical. R's basic data structures include the vector, list, matrix, data frame, and factors.


2 Answers

A vector has 1 dimension while a data frame has 2. I can't think of a good reason to convert a single vector into a data frame. That question generally arises when you have multiple vectors.

A better question is what is the difference between a data frame and a matrix: a data frame can have different data types for each column, while a matrix is all one data type. Behind the scenes, a data frame is really a list with equal length vectors at each index.

like image 199
Shane Avatar answered Sep 26 '22 15:09

Shane


Another good point to note is that when running code, operations on matrixes are (most of the time) much faster then on data frames.

Tal

like image 40
Tal Galili Avatar answered Sep 25 '22 15:09

Tal Galili