0

I want to get the length of a list, that's an element in another list.

first = c("Aa","Bbb")    
print(length(first))     # expect 2, gives 2 -> ok
texts = list(first,"Ccc",c("Dd","Eeeee","Fff")) # put elements into texts
print(length(texts))     # expect 3, gives 3 -> ok
print(lengths(texts))    #  expect 2 1 3, gives 2 1 3 -> ok
print(length(texts[1]))  #  expect 2, gives 1 -> ???

In the last line, I expect 2 = the length of element "first" but R gives 1. I do not understand, what happens. How can I get the correct value of the length of a specific element of the outer list?

Bartli
  • 315
  • 2
  • 11
  • 3
    `texts[1]` is still a list that contains a single vector. Use `length(texts[[1]])` or `lengths(texts[1])` – markus Jul 11 '19 at 08:22
  • 1
    Relevant: [The difference between bracket `[ ]` and double bracket `[[ ]]` for accessing the elements of a list or dataframe](https://stackoverflow.com/questions/1169456/the-difference-between-bracket-and-double-bracket-for-accessing-the-el) – markus Jul 11 '19 at 08:28

0 Answers0