每行一行打印 R 数据帧

发布于 2025-01-14 15:24:33 字数 112 浏览 3 评论 0 原文

当使用 print 打印数据帧时,我发现它会停在某个宽度,有点短于 80 个字符,并且只打印那些可以容纳的列。然后它继续打印接下来的许多列。

是否可以让它打印整行,无论多宽,没有换行符或点,输出?

When using print to print data frames I find that it stops at some width, a bit short of 80 characters, and prints only those columns that can fit. Then it proceeds to print the next so-many columns.

Is is possible to get it to print a whole row, regardless of how wide, without newlines or dots, to the output?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅黛梨妆こ 2025-01-21 15:24:33

您对另一个答案的评论表明您专门这样做是为了写入文件 - 这个答案就是这样做的,但根据您使用的是 R 还是 Rstudio,控制台中的显示可能会有所不同。

根据 ?options ,控制台有 10,000 个字符宽度的硬限制,只能通过重新编译 R 来增加。如果这足以满足您的用例,那么您可以这样做:

oldoptions <- options(width=10000)
options("width") #check that it's been set
#$width
#[1] 10000

sink("test.txt")  #append = TRUE might be useful for subsequent writes
print(a)  #Print the very wide dataframe
sink()

options(oldoptions) # reset the changed options

请注意,最初我当我在记事本中检查文件时,我认为这不起作用 - 那是因为记事本(我的设置)本身对行长度有限制,然后换行到下一行。使用更复杂的东西(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:

oldoptions <- options(width=10000)
options("width") #check that it's been set
#$width
#[1] 10000

sink("test.txt")  #append = TRUE might be useful for subsequent writes
print(a)  #Print the very wide dataframe
sink()

options(oldoptions) # reset the changed options

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.

帝王念 2025-01-21 15:24:33

如果您使用 df <- as_tibble(df) 将 data.frame 转换为 tibble,您就可以精细控制您想要打印表格的方式:

library(tidyverse)

# example data
data <- iris %>% t() %>% as_tibble()

# print all columns
data %>% print(width = Inf)

# print all rows and all columns
data %>% print(width = Inf, n = Inf)

另请参阅https://tibble.tidyverse.org/reference/formatting.html

但是,仅打印前行和前列是有原因的:打印数千行可能会变得非常混乱且缓慢。
您还可以运行 View(data) 在新窗口中浏览该表。

If you convert your data.frame to a tibble using df <- as_tibble(df), you have access to fine control the way you'd like to print your table:

library(tidyverse)

# example data
data <- iris %>% t() %>% as_tibble()

# print all columns
data %>% print(width = Inf)

# print all rows and all columns
data %>% print(width = Inf, n = Inf)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文