Write a named list of strings with variable length and number of strings in a text file in R

advertisements

I'm relatively new R user and still learning the basics.

I have a named list xx those entries looks like this:

> xx[100:105]
$`15LOX-1`
[1] "207328_at"

$`16.1`
[1] "215946_x_at"

$`16.2`
[1] NA

$`16.3A5`
[1] "200983_x_at" "200984_s_at" "200985_s_at" "212463_at"   "228748_at"  

$`160-KD`
[1] "201224_s_at" "201225_s_at"

$`1600019D15Rik`
[1] "218465_at"   "222642_s_at" "225492_at"   "235907_at"   "238831_at"

I would like to save it to a text file with two columns - Key and Value. If several strings correspond to the same key they should be in different rows. Double-quote symbols are not required.

In addition, how I can avoid NA values to be saved?

Please help.


Recreate test data:

xx <- structure(list(
    `15LOX-1` = "207328_at",
    `16.1` = "215946_x_at",
    `16.2` = NA,
    `16.3A5` = c("200983_x_at", "200984_s_at", "200985_s_at", "212463_at", "228748_at"),
    `160-KD` = c("201224_s_at", "201225_s_at" ),
    `1600019D15Rik` = c("218465_at", "222642_s_at", "225492_at", "235907_at", "238831_at")),
   .Names = c("15LOX-1", "16.1", "16.2", "16.3A5", "160-KD", "1600019D15Rik"))

First, remove all the NA values:

xx[is.na(xx)] <- NULL

Now, create a temporary variable that stores the length of each element in x:

tmp <- sapply(xx, function(xt)length(xt))

Now use rep to create the key (i.e. repeat the names of x, each time the length of the associated element), and use a combination of unlist and unname to create the values:

data.frame(
    key = rep(names(tmp), times=unname(tmp)),
    value = unlist(unname(xx))
)

This produces:

             key       value
1        15LOX-1   207328_at
2           16.1 215946_x_at
3         16.3A5 200983_x_at
4         16.3A5 200984_s_at
5         16.3A5 200985_s_at
6         16.3A5   212463_at
7         16.3A5   228748_at
8         160-KD 201224_s_at
9         160-KD 201225_s_at
10 1600019D15Rik   218465_at
11 1600019D15Rik 222642_s_at
12 1600019D15Rik   225492_at
13 1600019D15Rik   235907_at
14 1600019D15Rik   238831_at

Finally, use write.csv(x, file=...) or your favourite write function to save the data to file.