[R] creating a dataframe with full_join and looping over a list of lists.

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Fri Mar 22 00:12:40 CET 2019


Hi Matthew,
First thing, don't put:

mydf3 <- data.frame(myenter)

inside your loop, otherwise you will reset the value of mydf3 each
time and end up with only "myenter" and the final list. Without some
idea of the contents of comatgs, it is difficult to suggest a way to
get what you want.

Jim

On Fri, Mar 22, 2019 at 8:16 AM Matthew
<mccormack using molbio.mgh.harvard.edu> wrote:
>
>    My apologies, my first e-mail formatted very poorly when sent, so I am trying again with something I hope will be less confusing.
>
> I have been trying create a dataframe by looping through a list of lists,
> and using dplyr's full_join so as to keep common elements on the same row.
> But, I have a couple of problems.
>
> 1) The lists have different numbers of elements.
>
> 2) In the final dataframe, I would like the column names to be the names
> of the lists.
>
> Is it possible ?
>
> Code: *for(j in avector){****mydf3 <- data.frame(myenter) ****atglsts <-
> as.data.frame(comatgs[j]) ****mydf3 <- full_join(mydf3, atglsts) ****}*
> Explanation: # Start out with a list, myenter, to dataframe. mydf3 now
> has 1 column. # This first column will be the longest column in the
> final mydf3. # Loop through a list of lists, comatgs, and with each loop
> a particular list # is made into a dataframe of one column, atglsts. #
> The name of the column is the name of the list. # Each atglsts dataframe
> has a different number of elements. # What I want to do, is to add the
> newly made dataframe, atglsts, as a # new column of the data frame,
> mydf3 using full_join # in order to keep common elements on the same
> row. # I could rename the colname to 'AGI' so that I can join by 'AGI',
> # but then I would lose the name of the list. # In the final dataframe,
> I want to know the name of the original list # the column was made from. Matthew
>
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list