0

I'm currently working on a Homework, where I'm asked to subset a list of reviews to a new list containing only reviews with 5 or less words.

Using short_revs <- walk(mydoc, ~length(mydoc[[i]]) <= 5)) returns me the same initial List. Can anyone help?

  • `short_revs <- mydoc[ lengths(mydoc) <= 5 ]` or `short_revs <- Filter(function(z) length(z) <= 5, mydoc)`. The use of `purrr::walk` operates solely in side-effect and will always return the input unchanged. – r2evans Nov 07 '22 at 12:11
  • 1
    Thank you that worked immediately :) – Niclas Kammler Nov 07 '22 at 12:15
  • Greetings! In the future, usually it is helpful to provide a minimally reproducible dataset for questions here so people can troubleshoot your problems (rather than a table or screenshot for example). One way of doing this is by using the `dput` function on the data you are using and pasting the output into your question. You can find out how to use it here: https://youtu.be/3EID3P1oisg – Shawn Hemelstrand Nov 16 '22 at 06:47

1 Answers1

1

I think walk is not the right tool for this: it operates solely in side-effect, always returning the input unchanged. Some simple alternatives, choose one:

short_revs <- mydoc[ lengths(mydoc) <= 5 ]
short_revs <- Filter(function(z) length(z) <= 5, mydoc]
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • But will `length` really return the number of words? That probably returns the number of characters, right? I guess it depends on exactly what is in the list. – MrFlick Nov 07 '22 at 14:39
  • @MrFlick, I'm inferring that it's a list of character vectors. But no, `length` will not return the number of characters (unless a sentence was first string-split with an empty pattern, ala `strsplit("hello","")`). – r2evans Nov 07 '22 at 14:42
  • 1
    Oh yeah. I've been writing too much python lately. Forgive me. Plenty of options for counting words already exist here if it's one long string: https://stackoverflow.com/questions/8920145/count-the-number-of-all-words-in-a-string – MrFlick Nov 07 '22 at 15:02
  • 1
    I also jump between them frequently, though I'm on a heavy R-binge recently. – r2evans Nov 07 '22 at 15:07