将列表导出为R中的CSV或TXT文件 [英] Export a list into a CSV or TXT file in R

查看:443
本文介绍了将列表导出为R中的CSV或TXT文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉问这个问题。我非常喜欢R。我知道有很多线程与相同的问题有关。我明白,如果一个表的元素是列表,但是我没有解决我的问题,我们不能导出一个表。所以,我在R中有一个列表,我想将其导出为CSV或TXT文件。这是我执行这个 write.table 命令时会收到的错误消息:

  write.table(mylist,test.txt,sep =;)

.External2中的错误(C_writetable,x,file,nrow(x),p,rnames,在ecodeElement中,


这是我的列表的第一个元素:

  $ f10010_1 
$ f10010_1 $ mots
[1] X16 ESPRESSO TDISC TASSIMO
[5] CARTE NOIRE A LAVAZZA
[9] MALONGO MIO MODO 123
[13] CAPSULES DOSES 78G LONG
[17] SPRESSO CAFE 120G CLASSIC
[ 21] 104G 128G AROMATIQUE INTENSE
[25] 112G 156G 520G 5X16
[29] PROMO TRIPACK X24 126G
[33] 16 4X16 APPASSIONATAMENTE APPASSIONATEMENTE
[37] BRESIL CAPSUL COLOMBIE CORSE
[41] CREMOSAMENTE DELICATI DELIZIOSAMENTE DIVINAMENTE
[45 ] DOLCEMENTE EQI GRAND GRANDE
[49] GT GUATMALA HAITI INTENSAMENTE
[53]意大利MAGICAMENTE MERE MOKA78G
[57]赞助商产品PURSMATIN RESERVE
[61] RISTRETO SOAVEMENTE STYLE X36
64级别:104G 112G 120G 123 126G 128G 156G 16 4X16 520G 5X16 78G ... X36

$ f10010_1 $ nblabel
[1] 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
[27] 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 3 2 32 32 32 32 32 32 32 32 32 32
[53] 32 32 32 32 32 32 32 32 32 32 32 32
等级:32

$ f10010_1 $ Freq
[1] 18 16 16 15 14 14 9 9 9 9 8 8 8 7 7 7 6 5 5 3 3 3 3 2 2
[27] 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[53] 1 1 1 1 1 1 1 1 1 1 1 1

$ f10010_1 $ pct
[1] 0.56250 0.50000 0.50000 0.46875 0.43750 0.43750 0.28125 0.28125 0.28125
[10] 0.28125 0.28125 0.25000 0.25000 0.25000 0.21875 0.21875 0.21875 0.18750
[19] 0.15625 0.15625 0.09375 0.09375 0.09375 0.09375 0.06250 0.06250 0.06250
[28 ] 0.06250 0.06250 0.06250 0.06250 0.03125 0.03125 0.03125 0.03125 0.03125
[37] 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125
[46] 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125
[55] 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125
[64] 0.03125

谢谢y ou全部为您的帮助提前!

解决方案

所以基本上你有一个列表列表,mylist是主要的名字列表,第一个元素是 $ f10010_1 ,它被打印出来(并且包含4个列表)。



I认为最简单的方法是使用 lapply 添加 dataframe (假设每个元素内的每个列表的主列表(如 $ f10010_1 中的列表)的长度相同):

 code> lapply(mylist,function(x)write.table(data.frame(x),'test.csv',append = T,sep =','))

以上将将$ code> $ f10010_1 转换为数据帧,然后对每个其他元素执行相同操作并在'test.csv'中追加一个



您还可以在您的...上键入?write.table 控制台,当您将表格写入到其中时,检查您需要通过什么其他参数csv文件是否需要行名或列名等。


I'm sorry to ask this question. I'm very noob with R. I know there is a lot of threads which are related to the very same problem. I understood that we cannot export a table if one of its elements is a list but I didn't manage to solve my problem though. So, I got a list in R and I want to export it into a CSV or TXT file. Here is the error message that I get when I execute this write.table command :

write.table(mylist,"test.txt",sep=";")

Error in .External2(C_writetable, x, file, nrow(x), p, rnames, sep, eol,  : 
unimplemented type 'list' in 'EncodeElement'

Here is the first element of my list :

$f10010_1
$f10010_1$mots
 [1] X16               ESPRESSO          TDISC             TASSIMO          
 [5] CARTE             NOIRE             A                 LAVAZZA          
 [9] MALONGO           MIO               MODO              123              
[13] CAPSULES          DOSES             78G               LONG             
[17] SPRESSO           CAFE              120G              CLASSIC          
[21] 104G              128G              AROMATIQUE        INTENSE          
[25] 112G              156G              520G              5X16             
[29] PROMO             TRIPACK           X24               126G             
[33] 16                4X16              APPASSIONATAMENTE APPASSIONATEMENTE
[37] BRESIL            CAPSUL            COLOMBIE          CORSE            
[41] CREMOSAMENTE      DELICATI          DELIZIOSAMENTE    DIVINAMENTE      
[45] DOLCEMENTE        EQI               GRAND             GRANDE           
[49] GT                GUATEMALA         HAITI             INTENSAMENTE     
[53] ITALIAN           MAGICAMENTE       MERE              MOKA78G          
[57] PETITS            PRODUCT           PURSMATIN         RESERVE          
[61] RISTRETO          SOAVEMENTE        STYLE             X36              
64 Levels: 104G 112G 120G 123 126G 128G 156G 16 4X16 520G 5X16 78G ... X36

$f10010_1$nblabel
 [1] 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
[27] 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
[53] 32 32 32 32 32 32 32 32 32 32 32 32
Levels: 32

$f10010_1$Freq
 [1] 18 16 16 15 14 14  9  9  9  9  9  8  8  8  7  7  7  6  5  5  3  3  3  3  2  2
[27]  2  2  2  2  2  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
[53]  1  1  1  1  1  1  1  1  1  1  1  1

$f10010_1$pct
 [1] 0.56250 0.50000 0.50000 0.46875 0.43750 0.43750 0.28125 0.28125 0.28125
[10] 0.28125 0.28125 0.25000 0.25000 0.25000 0.21875 0.21875 0.21875 0.18750
[19] 0.15625 0.15625 0.09375 0.09375 0.09375 0.09375 0.06250 0.06250 0.06250
[28] 0.06250 0.06250 0.06250 0.06250 0.03125 0.03125 0.03125 0.03125 0.03125
[37] 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125
[46] 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125
[55] 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125
[64] 0.03125

Thank you all for your help in advance!

解决方案

So essentially you have a list of lists, with mylist being the name of the main list and the first element being $f10010_1 which is printed out (and which contains 4 more lists).

I think the easiest way to do this is to use lapply with the addition of dataframe (assuming that each list inside each element of the main list (like the lists in $f10010_1) has the same length):

lapply(mylist, function(x) write.table( data.frame(x), 'test.csv'  , append= T, sep=',' ))

The above will convert $f10010_1 into a dataframe then do the same with every other element and append one below the other in 'test.csv'

You can also type ?write.table on your console to check what other arguments you need to pass when you write the table to a csv file e.g. whether you need row names or column names etc.

这篇关于将列表导出为R中的CSV或TXT文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆