Re: [R] Reduce woes

2016-08-01 Thread Stefan Kruger
That seems like sage advise :) Thanks Stefan On 29 July 2016 at 22:06, Jeff Newmiller wrote: > Having experienced some frustration myself when I first started with R > many years ago, I can relate to your apparent frustration. However, if you > would like to succeed

Re: [R] Reduce woes

2016-07-29 Thread Jeff Newmiller
Having experienced some frustration myself when I first started with R many years ago, I can relate to your apparent frustration. However, if you would like to succeed in using R I strongly recommend learning R and not trying to write Haskell or Erlang or C or Fortran or any other language when

Re: [R] Reduce woes

2016-07-29 Thread Stefan Kruger
>> I still don't understand why you want Reduce to to lapply's >> job. Reduce maps many to one and lapply maps many to >> many. Say you want to map a function over a subset of a vector or list? With the generalised version of Reduce you map many-to-one, but the one can be a 'complex' structure.

Re: [R] Reduce woes

2016-07-29 Thread William Dunlap via R-help
Reduce (like lapply) apparently uses the [[ operator to extract components from the list given to it. X[[i]] does not attach names(X)[i] to its output (where would it put it?). Hence your se To help understand what these functions are doing try putting print statements in your test functions: >

Re: [R] Reduce woes

2016-07-29 Thread Stefan Kruger
Jeremiah - neat - that's one step closer, but one small thing I still don't understand: > data <- list(one = c(1, 1), three = c(3), two = c(2, 2)) > r = Reduce(function(acc, item) { append(acc, setNames(length(item), names(item))) }, data, list()) > str(r) List of 3 $ : int 2 $ : int 1 $ :

Re: [R] Reduce woes

2016-07-28 Thread jeremiah rounds
Basically using Reduce as an lapply in that example, but I think that was caused by how people started talking about things in the first place =) But the point is the accumulator can be anything as far as I can tell. On Thu, Jul 28, 2016 at 12:14 PM, jeremiah rounds

Re: [R] Reduce woes

2016-07-28 Thread jeremiah rounds
Re: "What I'm trying to work out is how to have the accumulator in Reduce not be the same type as the elements of the vector/list being reduced - ideally it could be an S3 instance, list, vector, or data frame." Pretty sure that is not true. See code that follows. I would never solve this task

Re: [R] Reduce woes

2016-07-28 Thread William Dunlap via R-help
I am not as familiar with those other languages as I should be, but in both examples aren't you using 'map' to call 'reduce' on each component of a list? In R the basic mapping function is lapply. > data <- list(one = c(1, 1), three = c(3), two = c(2, 2)) > rData <- lapply(X=data,

Re: [R] Reduce woes

2016-07-28 Thread Stefan Kruger
Ulrik - many thanks for your reply. I'm aware of many simple solutions as the one you suggest, both iterative and functional style - but I'm trying to learn how to bend Reduce() for the purpose of using it in more complex processing tasks. What I'm trying to work out is how to have the

Re: [R] Reduce woes

2016-07-28 Thread Ulrik Stervbo
Hi Stefan, in that case,lapply(data, length) should do the trick. Best wishes, Ulrik On Thu, 28 Jul 2016 at 12:57 Stefan Kruger wrote: > David - many thanks for your response. > > What I tried to do was to turn > > data <- list(one = c(1, 1), three = c(3), two = c(2,

Re: [R] Reduce woes

2016-07-28 Thread Stefan Kruger
David - many thanks for your response. What I tried to do was to turn data <- list(one = c(1, 1), three = c(3), two = c(2, 2)) into result <- list(one = 2, three = 1, two = 2) that is creating a new list which has the same names as the first, but where the values are the vector lengths. I

Re: [R] Reduce woes

2016-07-27 Thread David Winsemius
> On Jul 27, 2016, at 8:20 AM, Stefan Kruger wrote: > > Hi - > > I'm new to R. > > In other functional languages I'm familiar with you can often seed a call > to reduce() with a custom accumulator. Here's an example in Elixir: > > map = %{"one" => [1, 1], "three" =>

Re: [R] Reduce woes

2016-07-27 Thread Peter Langfelder
If you have a simple list of vectors (call it lst), use lengths = sapply(lst, length) In general, you may want to look at functions lapply and sapply which apply a function over a list, in this case the function length(). Peter On Wed, Jul 27, 2016 at 8:20 AM, Stefan Kruger

[R] Reduce woes

2016-07-27 Thread Stefan Kruger
Hi - I'm new to R. In other functional languages I'm familiar with you can often seed a call to reduce() with a custom accumulator. Here's an example in Elixir: map = %{"one" => [1, 1], "three" => [3], "two" => [2, 2]} map |> Enum.reduce(%{}, fn ({k,v}, acc) -> Map.update(acc, k, Enum.count(v),