将具有可变长度和字符串数量的命名字符串列表写入 R 中的文本文件
我是相对较新的 R 用户,仍在学习基础知识。
我有一个命名列表 xx ,这些条目如下所示:
> 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"
我想将其保存到具有两列的文本文件中 - 键和值。如果多个字符串对应相同的键,它们应该位于不同的行中。不需要双引号符号。
另外,如何避免保存 NA 值?
请帮忙。
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]
I'm relatively new R user and still learning the basics.
I have a named list xx
those entries looks like this:
15LOX-1`
[1] "207328_at"
I'm relatively new R user and still learning the basics.
I have a named list xx
those entries looks like this:
16.1`
[1] "215946_x_at"
I'm relatively new R user and still learning the basics.
I have a named list xx
those entries looks like this:
16.2`
[1] NA
I'm relatively new R user and still learning the basics.
I have a named list xx
those entries looks like this:
16.3A5`
[1] "200983_x_at" "200984_s_at" "200985_s_at" "212463_at" "228748_at"
I'm relatively new R user and still learning the basics.
I have a named list xx
those entries looks like this:
160-KD`
[1] "201224_s_at" "201225_s_at"
I'm relatively new R user and still learning the basics.
I have a named list xx
those entries looks like this:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Andrie 的测试数据,这里有一种使用
reshape
包(或“reshape2”)来完成此操作的巧妙方法:接下来,
melt
有一个列表方法!然后我们可以使用
complete.cases
或类似的东西取出NA
:Using Andrie's test data, here's a kind of slick way to do this using the
reshape
package (or `reshape2'):Next,
melt
has a list method!Then we can pull out the
NA
s usingcomplete.cases
or something equivalent:我会做这样的事情。
您可以查看 write.table 的帮助来查看其他可用选项。
询问的结果
这是 Andrie HTH
I would do something like this.
You can look at the help of write.table to see the other available options.
Here's the result as asked by Andrie
HTH
重新创建测试数据:
首先,删除所有 NA 值:
现在,创建一个临时变量,用于存储 x 中每个元素的长度:
现在使用
rep
创建键(即重复 x 的名称,每次关联元素的长度),并使用unlist
和unname
的组合来创建值:这会产生:
最后,使用
write.csv( x, file=...)
或您最喜欢的写入函数将数据保存到文件。Recreate test data:
First, remove all the NA values:
Now, create a temporary variable that stores the length of each element in x:
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 ofunlist
andunname
to create the values:This produces:
Finally, use
write.csv(x, file=...)
or your favourite write function to save the data to file.reshape2
包可以使用熔化函数来做到这一点。使用 Andrie 的数据:有些事情并不完全符合您的要求。首先,列的顺序相反,这可能是问题,也可能不是问题。其次,与列关联的名称并不完全符合您的要求,这也可能不是问题。第三,NA值仍然存在,这是一个基于你的问题的问题。我将使用 Complete.cases() 来解决该问题并为其指定一些适当的名称。也许是这样的:
reshape2
package can do this with the melt function. Using the data from Andrie:A few things are a not exactly as you want here. First, the columns are in reverse order which may or may not be an issue. Second, the names associated with the columns are not exactly as you wanted, again may not be an issue. Third, the NA value is still present, which is an issue based on your question. I'd use
complete.cases()
to address that issue and give it some appropriate names. Maybe something like this: