在数据集中设置列宽 [英] Setting column width in a data set

查看:80
本文介绍了在数据集中设置列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将此数据集中的列宽(对于所有3列)设置为:anim = 1-10; sireid = 11-20; damid = 21-30.有些列缺少值.

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)

推荐答案

从阅读您的问题以及对以前答案的评论以来,我看来您正在尝试创建固定宽度文件 >与您的数据.在这种情况下,可以在软件包gdata中使用函数write.fwf:

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)

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

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    

删除临时文件:

unlink(ff)

这篇关于在数据集中设置列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆