Horace  Wang
Horace Wang

Reputation: 11

Using lapply and str in R

When I input the following command in R.

lapply(list(1,TRUE),str)

It displays the following results:

num 1

 logi TRUE

[[1]]

NULL


[[2]]

NULL

I know num 1 and logi TRUE are the structure of each element. What is the meaning of the following part?

[[1]]

NULL


[[2]]

NULL

Upvotes: 0

Views: 262

Answers (3)

Frish Vanmol
Frish Vanmol

Reputation: 354

In addition to the previous answers, it could be useful to use

invisible(lapply(list(dt1,dt2...), str)) 

so you don't print anything more than the structure of the objects.

Upvotes: 0

Shawn Mehan
Shawn Mehan

Reputation: 4568

The [[N]] output means

  1. The first element of this list is a vector with a single element NULL

  2. the second element of this list is a vector with a single element NULL

Appending the [[N]] to the variable that holds the list actually returns the element.

Upvotes: 0

Vandenman
Vandenman

Reputation: 3176

This happens because the return value of str is NULL. Consider:

a <- str(list(1,TRUE))
a # NULL

Now because you use lapply, lapply will return a list with an equal number of elements as the input list. In your case, this is a list of two elements that are both NULL.

Upvotes: 2

Related Questions