What is the difference between a data framework and a list in R?

What is the difference between a file frame and a list in R ? Which one should be used when? Which is easier to sort out?

Exact problem: I must first save 3 string elements like "a", "b", "c". Later for each of them I need to add 3 more elements; for example, for "a" I have to add "a1", "a2", "a3". Later I have to use nested loops to access these elements.

So, am I confused to use a dataframe or a list or some other type of data in which I could first save and then add (the look of each column)?

I am currently getting errors, for example "the number of elements to replace is not a multiple of the length of the replacement"

+51
list r dataframe
Apr 09 '13 at 11:56
source share
2 answers

The question is not as stupid as some people think. I know a lot of people struggling with this difference, and what to use where. Summarizing:

Lists are by far the most flexible data structure in R. They can be considered as a collection of elements without any restrictions on the class, length or structure of each element. The only thing you need to take care of is that you are not giving the two elements the same name. This can cause a lot of confusion, and R gives no errors for this:

 > X <- list(a=1,b=2,a=3) > X$a [1] 1 

Data frames are also lists, but they have several limitations:

  • you cannot use the same name for two different variables
  • all elements of the data frame are vectors
  • all elements of the data frame are of equal length.

Due to these limitations and the resulting two-dimensional structure, data frames can mimic some of the properties of matrices. You can select rows and perform operations on rows. You cannot do this with lists, since there is an undefined string.

All of this means that you must use a data frame for any data set that fits into this two-dimensional structure. In essence, you use data frames for any data set where the column matches a variable and the row matches one observation in the broad sense of the word. For all other structures, lists are the way to go.

Note that if you want a nested structure, you need to use lists. Since list items can be lists themselves, you can create very flexible structured objects.

+90
Apr 09 '13 at 13:14
source share

Take a look at an example: If you use apply instead of sapply to get a class -

 apply(iris,2,class) # function elements are rows or columns Sepal.Length Sepal.Width Petal.Length Petal.Width Species "character" "character" "character" "character" "character" sapply(iris,class) # function elements are variables Sepal.Length Sepal.Width Petal.Length Petal.Width Species "numeric" "numeric" "numeric" "numeric" "factor" 
0
Aug 12 '15 at 17:26
source share



All Articles