[R] Confused

Peter Ehlers ehlers at ucalgary.ca
Mon Feb 7 09:48:39 CET 2011


On 2011-02-07 00:18, Joel wrote:
>
> Hi
>
> Im confused by one thing, and if someone can explain it I would be a happy
>
>> rev(strsplit("hej",NULL))
> [[1]]
> [1] "h" "e" "j"
>
>> lapply(strsplit("hej",NULL),rev)
> [[1]]
> [1] "j" "e" "h"
>
> Why dossent the first one work? What is it in R that "fails" so to say that
> you need to use lapply for it to get the correct output.

See if this helps to see what's happening in the first case:

  L <- list(fruit=c("apple", "orange"))
  L
  rev(L)

  L <- list(fruit=c("apple", "orange"), nuts=c("pecan", "almond"))
  L
  rev(L)

  lapply(L, rev)

For your second case, lapply() applies FUN to the pieces
of the list.

Peter Ehlers



More information about the R-help mailing list