Use of C or List

I am learning R and trying to understand the difference between c() and list(). Both seem to be able to store multiple elements, but I noticed they behave differently in certain cases.

  1. When should I use c() vs. list()?
  2. How do they handle mixed data types differently?
  3. What happens when I nest lists or combine lists using c()?"*

Could someone provide a clear explanation with examples? Thank you!

In R, lists can combine items of different types whereas vector entries must all have the same type. If you use list(), the result is unsurprisingly a list.

If you use c(), the result is (to quote the help entry for c()) "the highest type of the components in the hierarchy NULL < raw < logical < integer < double < complex < character < list < expression". So, for example, c(3, "joe") results is a character vector ("3", "joe") because you can convert numbers to strings but not vice versa. c(3, "joe", list(a = 2, b = 4)) results in a list with four entries.

Enter ?c at the R prompt to see the full documentation. Personally, I only use c() with arguments of the same type, or to add entries to a list.

1 Like

To try to make more explicit a fundamental difference: when you have some data, let's say a variable that was measured repeatedly in different conditions, you almost always want to store it in a vector, using c() or similar.

In that case, you are not mixing data types, and it would be meaningless: in a given vector you only store measurements of the same variable, so it's always the same type.

In R, every variable is a vector, and for data/statistics purpose, you could get away with only just vectors. But when you have several variables that you need to store at the same time, it's sometimes convenient to group them together, in that case lists are handy. In the vast majority of cases, in R, you don't work with data in lists, you just use lists to keep the data organized.