PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, December 14, 2022

[FIXED] How can I use the dollar sign operator $ to refer to elements of lists *inside* a list in R (or a tibble column if that's different)?

 December 14, 2022     dollar-sign, list, r, syntax, tibble     No comments   

Issue

I can enter something like

list(3, 4, 5, 4, 3, 2, 2, 3, 2, 3, 4, 1, 1, 2, 1, 3) == 3

and get back

TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE

which is great because it means that I don't have to clumsily use lapply() to get R to do what I want. But what if I have a list of lists, where the inner lists have named elements. For example, I could have a list of books, where each book is a list consisting of a $title and a $year in which it was published. Let's say I have a tibble called books with just 1 column called book that is this list of books. How would I filter for only books published before 2000, or mutate() a column containing the titles?

The naive approach I would take to trying to filter by year is:

filter(books, book$year < 2000)

because I want it to go through each element of books$book and then look at the $year element of that, but, since books$book is itself a list, R looks for something named books$book$year instead of looking for the $year element of each element inside of books$book. How do I get around this without using lapply()?

Just for completeness, here is the (somewhat clumsy looking) way I do it with lapply():

filter(books, as.logical(lapply(books$book, function(x) x$year < 2000)))


Solution

You can use the code that you provide above, but you can simplify it to this:

filter(books,sapply(book, \(x) x$year<2000))

Instead of

filter(books, as.logical(lapply(books$book, function(x) x$year < 2000)))

The two changes are:

  • use sapply() instead of lapply() which will obviate the need for as.logical()
  • use the \(x) shortcut for function(x)

If you really don't want to use lapply/sapply, you can, as @akrun suggests, switch to map_lgl, although with a slight simplification:

filter(books, map_lgl(book,~x$year<2000))


Answered By - langtang
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing