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

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Fri Mar 22 01:36:47 CET 2019


1. This is a plain text list. Do not post in HTML.

2. Read the posting guide. You will enhance your chnce of getting a useful
reply if you do what it says, especially prividing a reproducible example
that shows what you have, what you want, and the code you used, along with
any error messages you received.

Please read the ?data.frame or a tutorial on data frames. All columns
*must* have the same length. So what it comes down to is probably how you
want to fill with NA's.

Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Mar 21, 2019 at 1:58 PM Matthew <mccormack using molbio.mgh.harvard.edu>
wrote:

> 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 ?
>
>
>   for(j in avector){
>
>      mydf3 <- data.frame(myenter)           # 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.
>      atglsts <- as.data.frame(comatgs[j])  # 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.
>      mydf3 <- full_join(mydf3, atglsts)     # 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list