繁体   English   中英

将列表转换为数据框

[英]Convert list to data frame

我面临在我的data.frame中有一个列表的问题:

> str(df.res)
'data.frame':   10 obs. of  5 variables:
 $ cryptodf.linksdf.k.   : Factor w/ 1178 levels "https://coinmarketcap.com/currencies/0x/",..: 126 405 883 121 637 301 721 130 722 698
 $ cryptodf.symboldf.k.  : Factor w/ 1164 levels "$$$","020","1337",..: 166 378 1125 98 592 277 1095 96 676 1108
 $ cryptodf.coinnamedf.k.: Factor w/ 1175 levels "020LondonCoin",..: 121 401 881 123 633 302 720 131 721 697
 $ website1              :List of 10
  ..$ : chr "<a href=\"https://bitcoin.org/\" target=\"_blank\">Website</a>\n                <a href=\"https://www.bitcoin.com/\" target=\"_"| __truncated__
  ..$ : chr "<a href=\"https://www.ethereum.org/\" target=\"_blank\">Website</a>\n                \n                <a href=\"https://live.e"| __truncated__
  ..$ : chr "<a href=\"https://ripple.com/\" target=\"_blank\">Website</a>\n                \n                <a href=\"https://ripple.com/g"| __truncated__
  ..$ : chr "<a href=\"https://www.bitcoincash.org/\" target=\"_blank\">Website</a>\n                \n                <a href=\"https://blo"| __truncated__
  ..$ : chr "<a href=\"https://litecoin.com\" target=\"_blank\">Website</a>\n                <a href=\"https://litecoin.org/\" target=\"_bla"| __truncated__
  ..$ : chr "<a href=\"https://www.dash.org/\" target=\"_blank\">Website</a>\n                \n                <a href=\"https://chainz.cry"| __truncated__
  ..$ : chr "<a href=\"http://nem.io\" target=\"_blank\">Website</a>\n                \n                <a href=\"http://nembex.nem.ninja/\""| __truncated__
  ..$ : chr "<a href=\"https://bitconnectcoin.co/\" target=\"_blank\">Website</a>\n                \n                <a href=\"https://chain"| __truncated__
  ..$ : chr "<a href=\"https://neo.org/\" target=\"_blank\">Website</a>\n                \n                <a href=\"https://neotracker.io\""| __truncated__
  ..$ : chr "<a href=\"http://www.monero.cc\" target=\"_blank\">Website</a>\n                \n                <a href=\"http://moneroblocks"| __truncated__
 $ logo                  : Factor w/ 10 levels "https://files.coinmarketcap.com/static/img/coins/32x32/bitcoin.png",..: 1 2 3 4 5 6 7 8 9 10
> 

如果我想将data.frame写入csv,则会得到:

> write.table(df.res, file = path, row.names=FALSE, sep=";")
Error in .External2(C_writetable, x, file, nrow(x), p, rnames, sep, eol,  : 
  unimplemented type 'list' in 'EncodeElement'

我尝试重命名列,也尝试重命名unlist函数,但是这两种方式都给我带来了错误的格式。 我想简单地将每个列表项放在一行中,作为其他因素。

有什么建议我做错了吗?

UDPATE

请在website列的dput()输出下面找到:

     > dput(head(df.res))
structure(list(cryptodf.linksdf.k. = c("https://coinmarketcap.com/currencies/bitcoin/", 
"https://coinmarketcap.com/currencies/ethereum/", "https://coinmarketcap.com/currencies/ripple/", 
"https://coinmarketcap.com/currencies/bitcoin-cash/", "https://coinmarketcap.com/currencies/litecoin/", 
"https://coinmarketcap.com/currencies/dash/"), cryptodf.symboldf.k. = c("BTC", 
"ETH", "XRP", "BCH", "LTC", "DASH"), cryptodf.coinnamedf.k. = c("Bitcoin", 
"Ethereum", "Ripple", "BitcoinCash", "Litecoin", "Dash"), website1 = c("<a href=\"https://bitcoin.org/\" target=\"_blank\">Website</a>\n                <a href=\"https://www.bitcoin.com/\" target=\"_blank\">Website 2</a>\n                <a href=\"http://blockchain.info\" target=\"_blank\">Explorer</a>\n                <a href=\"https://blockexplorer.com/\" target=\"_blank\">Explorer 2</a>\n                \n                   <a href=\"https://bitcointalk.org\" target=\"_blank\">Message Board</a>\n                   <a href=\"https://forum.bitcoin.com/\" target=\"_blank\">Message Board 2</a>\n                \n\n                \n               \n                    <small><span class=\"label label-success\"> Rank 1</span></small>\n                \n                \n                \n                \n                    \n                        <small><span class=\"label label-warning\">Mineable</span></small>\n                    \n                        <small><span class=\"label label-warning\">Coin</span></small>\n                    \n                    \n                \n                \n            ", 
"<a href=\"https://www.ethereum.org/\" target=\"_blank\">Website</a>\n                \n                <a href=\"https://live.ether.camp/\" target=\"_blank\">Explorer</a>\n                <a href=\"https://etherscan.io/\" target=\"_blank\">Explorer 2</a>\n                <a href=\"https://etherchain.org/\" target=\"_blank\">Explorer 3</a>\n                   <a href=\"https://forum.ethereum.org/\" target=\"_blank\">Message Board</a>\n                \n                <li><span class=\"glyphicon glyphicon-bullhorn text-gray\" title=\"Announcement\"/>    <a href=\"https://bitcointalk.org/index.php?topic=428589.0\" target=\"_blank\">Announcement</a>\n\n                \n               \n                    <small><span class=\"label label-success\"> Rank 2</span></small>\n                \n                \n                \n                \n                    \n                        <small><span class=\"label label-warning\">Mineable</span></small>\n                    \n                        <small><span class=\"label label-warning\">Coin</span></small>\n                    \n                    \n                \n                \n            ", 
"<a href=\"https://ripple.com/\" target=\"_blank\">Website</a>\n                \n                <a href=\"https://ripple.com/graph\" target=\"_blank\">Explorer</a>\n                <a href=\"https://bithomp.com/explorer/\" target=\"_blank\">Explorer 2</a>\n                \n                   <a href=\"http://www.xrpchat.com/\" target=\"_blank\">Message Board</a>\n                \n                \n\n                \n               \n                    <small><span class=\"label label-success\"> Rank 3</span></small>\n                \n                \n                \n                \n                    \n                        <small><span class=\"label label-warning\">Coin</span></small>\n                    \n                    \n                \n                \n            ", 
"<a href=\"https://www.bitcoincash.org/\" target=\"_blank\">Website</a>\n                \n                <a href=\"https://blockchair.com/bitcoin-cash/blocks\" target=\"_blank\">Explorer</a>\n                <a href=\"http://blockdozer.com/insight/\" target=\"_blank\">Explorer 2</a>\n                <a href=\"https://www.blocktrail.com/BCC\" target=\"_blank\">Explorer 3</a>\n                \n                \n                <li><span class=\"glyphicon glyphicon-bullhorn text-gray\" title=\"Announcement\"/>    <a href=\"https://bitcointalk.org/index.php?topic=2040221.0\" target=\"_blank\">Announcement</a>\n\n                \n               \n                    <small><span class=\"label label-success\"> Rank 4</span></small>\n                \n                \n                \n                \n                    \n                        <small><span class=\"label label-warning\">Mineable</span></small>\n                    \n                        <small><span class=\"label label-warning\">Coin</span></small>\n                    \n                    \n                \n                \n            ", 
"<a href=\"https://litecoin.com\" target=\"_blank\">Website</a>\n                <a href=\"https://litecoin.org/\" target=\"_blank\">Website 2</a>\n                <a href=\"http://explorer.litecoin.net/chain/Litecoin\" target=\"_blank\">Explorer</a>\n                <a href=\"https://chainz.cryptoid.info/ltc/\" target=\"_blank\">Explorer 2</a>\n                <a href=\"https://live.blockcypher.com/ltc/\" target=\"_blank\">Explorer 3</a>\n                   <a href=\"https://litecointalk.io/\" target=\"_blank\">Message Board</a>\n                \n                <li><span class=\"glyphicon glyphicon-bullhorn text-gray\" title=\"Announcement\"/>    <a href=\"https://bitcointalk.org/index.php?topic=47417.0\" target=\"_blank\">Announcement</a>\n\n                \n               \n                    <small><span class=\"label label-success\"> Rank 5</span></small>\n                \n                \n                \n                \n                    \n                        <small><span class=\"label label-warning\">Mineable</span></small>\n                    \n                        <small><span class=\"label label-warning\">Coin</span></small>\n                    \n                    \n                \n                \n            ", 
"<a href=\"https://www.dash.org/\" target=\"_blank\">Website</a>\n                \n                <a href=\"https://chainz.cryptoid.info/dash/\" target=\"_blank\">Explorer</a>\n                \n                \n                   <a href=\"https://www.dash.org/forum/\" target=\"_blank\">Message Board</a>\n                \n                <li><span class=\"glyphicon glyphicon-bullhorn text-gray\" title=\"Announcement\"/>    <a href=\"https://bitcointalk.org/index.php?topic=421615.0\" target=\"_blank\">Announcement</a>\n\n                \n               \n                    <small><span class=\"label label-success\"> Rank 6</span></small>\n                \n                \n                \n                \n                    \n                        <small><span class=\"label label-warning\">Mineable</span></small>\n                    \n                        <small><span class=\"label label-warning\">Coin</span></small>\n                    \n                    \n                \n                \n            "
), logo = structure(1:6, .Label = c("https://files.coinmarketcap.com/static/img/coins/32x32/bitcoin.png", 
"https://files.coinmarketcap.com/static/img/coins/32x32/ethereum.png", 
"https://files.coinmarketcap.com/static/img/coins/32x32/ripple.png", 
"https://files.coinmarketcap.com/static/img/coins/32x32/bitcoin-cash.png", 
"https://files.coinmarketcap.com/static/img/coins/32x32/litecoin.png", 
"https://files.coinmarketcap.com/static/img/coins/32x32/dash.png", 
"https://files.coinmarketcap.com/static/img/coins/32x32/nem.png", 
"https://files.coinmarketcap.com/static/img/coins/32x32/bitconnect.png", 
"https://files.coinmarketcap.com/static/img/coins/32x32/neo.png", 
"https://files.coinmarketcap.com/static/img/coins/32x32/monero.png"
), class = "factor")), .Names = c("cryptodf.linksdf.k.", "cryptodf.symboldf.k.", 
"cryptodf.coinnamedf.k.", "website1", "logo"), row.names = c("hrefs1", 
"hrefs2", "hrefs3", "hrefs4", "hrefs5", "hrefs6"), class = "data.frame")

您的website1列中有换行符。 您可以将其替换为另一个字符,例如

df.res[,"website1"] <- gsub("\\n", "; ", df.res[,"website1"])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM