设置数据集中的列宽

发布于 2024-12-11 06:44:52 字数 265 浏览 0 评论 0原文

我想在此数据集中设置列宽(所有 3 列),如下:anim=1-10;父辈=11-20;达米德=21-30。某些列缺少值。

anim=c("1A038","1C467","2F179","38138","030081")
sireid=c("NA","NA","1W960","1W960","64404")
damid=c("NA","NA","1P119","1P119","63666")

mydf=data.frame(anim,sireid,damid)

I would like to set column widths (for all the 3 columns) in this data set, as: anim=1-10; sireid=11-20; damid=21-30. Some columns have missing values.

anim=c("1A038","1C467","2F179","38138","030081")
sireid=c("NA","NA","1W960","1W960","64404")
damid=c("NA","NA","1P119","1P119","63666")

mydf=data.frame(anim,sireid,damid)

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

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

发布评论

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

评论(3

墟烟 2024-12-18 06:44:52

从阅读您的问题以及您对之前答案的评论,在我看来,您正在尝试使用您的数据创建一个固定宽度文件。如果是这种情况,您可以使用包 gdata 中的函数 write.fwf

加载包并创建临时输出文件:

library(gdata)
ff <- tempfile()

将数据以固定宽度格式写入到临时文件:

write.fwf(mydf, file=ff, width=c(10,10,10), colnames=FALSE)

使用 scan 读取文件并打印结果(以演示固定宽度输出):

zz <- scan(ff, what="character", sep="\n")
cat(zz, sep="\n")

1A038      NA         NA        
1C467      NA         NA        
2F179      1W960      1P119     
38138      1W960      1P119     
030081     64404      63666    

删除临时文件:

unlink(ff)

From reading your question as well as your comments to previous answers, it seems to me that you are trying to create a fixed width file with your data. If this is the case, you can use the function write.fwf in package gdata:

Load the package and create a temporary output file:

library(gdata)
ff <- tempfile()

Write your data in fixed width format to the temporary file:

write.fwf(mydf, file=ff, width=c(10,10,10), colnames=FALSE)

Read the file with scan and print the results (to demonstrate fixed width output):

zz <- scan(ff, what="character", sep="\n")
cat(zz, sep="\n")

1A038      NA         NA        
1C467      NA         NA        
2F179      1W960      1P119     
38138      1W960      1P119     
030081     64404      63666    

Delete the temporary file:

unlink(ff)
旧城烟雨 2024-12-18 06:44:52

您还可以使用 sprintf() 函数为数字和字符串编写固定宽度的输出,该函数派生自 C 的对应函数。

例如,用 0 填充整数:

sprintf("%012d",99)

用空格填充:
sprintf("%12d",123)

并填充字符串:

sprintf("%20s","hello world")

格式化选项可通过 ?sprintf 找到,并且有许多关于将 C 输出格式化为固定宽度的指南。

You can also write fixed width output for numbers and strings using the sprintf() function, which derives from C's counterpart.

For instance, to pad integers with 0s:

sprintf("%012d",99)

To pad with spaces:
sprintf("%12d",123)

And to pad strings:

sprintf("%20s","hello world")

The options for formatting are found via ?sprintf and there are many guides to formatting C output for fixed width.

黑寡妇 2024-12-18 06:44:52

听起来您来自 SAS 背景,其中字符变量应指定显式长度以避免意外截断。在R中,你不需要担心这个。字符串具有所需的字符数,并随着其内容的变化而自动扩展和收缩。

不过,您应该注意的一件事是将字符变量静默转换为数据框中的因子。但是,除非您稍后更改内容,否则您应该能够接受默认值。

It sounds like you're coming from a SAS background, where character variables should have explicit lengths specified to avoid unexpected truncations. In R, you don't need to worry about this. A character string has exactly as many characters as it needs, and automatically expands and contracts as its contents change.

One thing you should be aware of, though, is silent conversion of character variables to factors in a data frame. However, unless you change the contents at a later point in time, you should be able to live with the default.

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