每行一行打印 R 数据帧
当使用 print 打印数据帧时,我发现它会停在某个宽度,有点短于 80 个字符,并且只打印那些可以容纳的列。然后它继续打印接下来的许多列。
是否可以让它打印整行,无论多宽,没有换行符或点,输出?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
当使用 print 打印数据帧时,我发现它会停在某个宽度,有点短于 80 个字符,并且只打印那些可以容纳的列。然后它继续打印接下来的许多列。
是否可以让它打印整行,无论多宽,没有换行符或点,输出?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
您对另一个答案的评论表明您专门这样做是为了写入文件 - 这个答案就是这样做的,但根据您使用的是 R 还是 Rstudio,控制台中的显示可能会有所不同。
根据
?options
,控制台有 10,000 个字符宽度的硬限制,只能通过重新编译 R 来增加。如果这足以满足您的用例,那么您可以这样做:请注意,最初我当我在记事本中检查文件时,我认为这不起作用 - 那是因为记事本(我的设置)本身对行长度有限制,然后换行到下一行。使用更复杂的东西(Notepad++)查看表明它确实工作成功。
Your comment on another answer suggests that you're doing this specifically to write out to a file - this answer does that, but the display may look different in the console depending on whether you're using R or Rstudio.
According to
?options
there is a hard limit of 10,000 character width for the console, which can only be increased by recompiling R. If this is enough for your use case then you can do:Note that initially I thought that this wasn't working when I inspected the file in Notepad - that was because (my setup of) Notepad has a limit on line length itself, and then wraps onto the next line. Viewing with something more sophisticated (Notepad++) shows that it did work sccessfully.
如果您使用
df <- as_tibble(df)
将 data.frame 转换为tibble
,您就可以精细控制您想要打印表格的方式:另请参阅https://tibble.tidyverse.org/reference/formatting.html
但是,仅打印前行和前列是有原因的:打印数千行可能会变得非常混乱且缓慢。
您还可以运行
View(data)
在新窗口中浏览该表。If you convert your data.frame to a
tibble
usingdf <- as_tibble(df)
, you have access to fine control the way you'd like to print your table:See also https://tibble.tidyverse.org/reference/formatting.html
However, there is a reason to only print the first rows and columns: It can get very messy and slow to print thousands of rows.
You can also run
View(data)
to explore the table in a new window.