0

I am new to R and just started working with list. I have a list of different lengths that I would like to convert into a data frame. My list looks something like this:

List of A:  
$ reference: chr [1:20000] "13000" "5000" "23234" ...  
$ name_1 : chr [1:9000] "5000" "14523" ...  
$ name_2 : chr [1:800] "13000" "23234" ...

And I would like to make a data frame of something like this:

reference name_1 name_2  
13000     NA     13000  
5000      5000   NA  
23234     NA     23234

Thank you so much for the help! I tried to fill the rest of the column with NAs so they have the same length as the "reference" but it still did not organize it the way I wanted it!

  • 1
    How do you decide where NA values go? I don't see how the 9,000 name_1 values are supposed to be matched up to the 20,000 reference values. When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. (a `str()` isn't as helpful as a `dput()`) – MrFlick Feb 27 '18 at 15:43
  • Hey! Thanks for the response. Sorry if I wasn't being clear. name_1 and name_2 contain different sets of numbers (not sequential) and the reference contains all of the unique numbers in name_1 and name_2 (plus additional numbers). The NA values go to the values that are absent in name_1 or name_2 in regards to the reference (if that makes sense?). I hope this makes more sense? And I'll keep your suggestion in mind re. the reproducible example. Thanks a bunch. – snoopydoopy Feb 27 '18 at 15:55

2 Answers2

0

Something like this ?

A <- list()
A$reference <- c("13000","5000","23234")
A$name_1 <- c("5000","14523")
A$name_2 <- c("13000","23234")

B <- data.frame(reference = A$reference,
                name1 = ifelse(A$reference %in% A$name_1,A$reference,NA),
                name2 = ifelse(A$reference %in% A$name_2,A$reference,NA))

#   reference name1 name2
# 1     13000  <NA> 13000
# 2      5000  5000  <NA>
# 3     23234  <NA> 23234
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
0

if all the values of name_1 and name_2 are in ref then you can do something like this:

mapply(function(x,y){is.na(df$ref)<-!x%in%y;df$ref},df["ref"],df)
      ref <NA> <NA>
 [1,]   1    1   NA
 [2,]   2   NA    2
 [3,]   3    3   NA
 [4,]   4   NA    4
 [5,]   5    5   NA
 [6,]   6   NA    6
 [7,]   7    7   NA
 [8,]   8   NA    8
 [9,]   9    9   NA
[10,]  10   NA   10
[11,]  11   NA   NA
[12,]  12   NA   NA
[13,]  13   NA   NA
[14,]  14   NA   NA
[15,]  15   NA   NA

DATA

df=list(ref=1:15,name1=c(1,3,5,7,9),name2=1:5*2)

 df
$ref
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

$name1
[1] 1 3 5 7 9

$name2
[1]  2  4  6  8 10
Onyambu
  • 67,392
  • 3
  • 24
  • 53