-1

I am trying to do something like: I have this dataframe

Name   Distance  
James  2
James  1
Maria  5
Thomas 2
Thomas 1
James  4

I also have the dataframe that contais all the names, if it helps.

And retrieve 3 separate dataframes that should look like this:

   James  2 1 4
   Thomas 2 1
   Maria  5

Notice that I don't want to sum it, just get the values and I the list is much longer of names, so doing one by one is not optimal.

L8call
  • 87
  • 1
  • 1
  • 6
  • Are you referring to the conversion of a data frame from a long format to a wide format? – DTYK Jun 14 '18 at 05:30
  • *"And retrieve 3 separate dataframes"* So the output should be three *separate* `data.frame`s with one row each? I'm confused because your expected output looks like a single `data.frame`. – Maurits Evers Jun 14 '18 at 05:31
  • `split(df$Distance,df$Name)` does not return a data.frame but does seem to make sense here. Does that help? – Florian Jun 14 '18 at 05:32
  • Or perhaps something like this `aggregate(Distance ~ Name, df, paste0, collapse = " ")`? – Maurits Evers Jun 14 '18 at 05:33
  • I want diferent data.frames since they will have diferent dimensions... @MauritsEvers It is almost what I want, but the results are all in a place, and I need to further treat them – L8call Jun 14 '18 at 05:45
  • @Florian split could be a solution, can you please elaborate how can I use it and save the diferent data frames with diferent names ? – L8call Jun 14 '18 at 05:49
  • @DTYK cant understand your question – L8call Jun 14 '18 at 05:49
  • @L8call I have added my suggestion as an answer, since it might be a bit too long for the comments. – Florian Jun 14 '18 at 05:54
  • @RonakShah it is not the samething as I dont have a timevar – L8call Jun 14 '18 at 05:56

1 Answers1

1

My advice would be to not store the results in a data.frame but in a list, since the results will have different dimensions. You can obtain the desired list with

my_list = split(df$Distance,df$Name)

which returns:

$`James`
[1] 2 1 4

$Maria
[1] 5

$Thomas
[1] 2 1

Then for example, you can get the result for a single person as

my_list[['James']]
> 2 1 4

or get the sum for each person with

lapply(my_list,sum)

which returns:

$`James`
[1] 7

$Maria
[1] 5

$Thomas
[1] 3

Hope this helps!

Florian
  • 24,425
  • 4
  • 49
  • 80