Matthew
2019-Mar-21  20:58 UTC
[R] creating a dataframe with full_join and looping over a list of lists
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]]
Bert Gunter
2019-Mar-22  00:36 UTC
[R] creating a dataframe with full_join and looping over a list of lists
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 at 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 at 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]]