设置数据集中的列宽
我想在此数据集中设置列宽(所有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从阅读您的问题以及您对之前答案的评论,在我看来,您正在尝试使用您的数据创建一个固定宽度文件。如果是这种情况,您可以使用包
gdata
中的函数write.fwf
:加载包并创建临时输出文件:
将数据以固定宽度格式写入到临时文件:
使用
scan
读取文件并打印结果(以演示固定宽度输出):删除临时文件:
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 packagegdata
:Load the package and create a temporary output file:
Write your data in fixed width format to the temporary file:
Read the file with
scan
and print the results (to demonstrate fixed width output):Delete the temporary file:
您还可以使用
sprintf()
函数为数字和字符串编写固定宽度的输出,该函数派生自 C 的对应函数。例如,用 0 填充整数:
用空格填充:
sprintf("%12d",123)
并填充字符串:
格式化选项可通过
?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:
To pad with spaces:
sprintf("%12d",123)
And to pad strings:
The options for formatting are found via
?sprintf
and there are many guides to formatting C output for fixed width.听起来您来自 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.