当header = TRUE时,read.fwf出错

编程入门 行业动态 更新时间:2024-10-23 08:28:27
本文介绍了当header = TRUE时,read.fwf出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有模拟的数据,如下所示:

I have simulated data that looks like this:

LastName Date email CreditCardNum AgeZip Amount Paul 21/02/14 Aliquam.fringilla@dolordapibus.co.uk 4241033422900360 6738851$14.39 Bullock 2/7/2014adipiscing.fringilla@lectusjusto 5178789953524240 3336538$498.31 Mcmahon 11/5/2013lobortis.ultrices@lacus 5389589582467450 7734302$92.44 Walters 25/09/13 consectetuer.cursus.et@sitamet 5157094536097720 7794007$206.60 Franco 17/06/13 et@disparturientmontes.ca 345477952996264 2415873$89.12

这就是我试图将它导入R的方式,包括标题:

and this is how I'm attempting to import it into R, with headers:

w <- c(11,10,57,16,3,5,8) df <- read.fwf("data.txt",widths=w,stringsAsFactors=F) names(df) <- df[1,]; df <- df[-1,]

我之所以不使用 header = T 是它给我的错误:

The reason I'm not using header=T is that it gives me the error:

Error in read.table(file = FILE, header = header, sep = sep, row.names = row.names, : more columns than column names

这不是真的。我知道宽度( w )是正确的。那么这个错误来自哪里?我的解决方案工作正常,我' d只是想了解发生了什么。

which just isn't true. I know the widths (w) are correct. So where is this error coming from? My solution works fine, I'd just like to understand what's happening.

推荐答案

如果指定 header = TRUE ,然后,根据?read.fwf ,您必须确保列名由 sep 分隔。默认情况下,名称由 \t (制表符)分隔,对于您的数据,这不能为真。

If you specify header=TRUE, then, as per ?read.fwf, you must ensure that the column names are separated by sep. The default is for names to be separated by \t (the tab character) and this must not be true for your data.

以下工作正常:

w <- c(11, 10, 57, 16, 3, 5, 8) read.fwf(widths=w, header=TRUE, sep='|', file=textConnection('LastName |Date |email |CreditCardNum |Age|Zip |Amount Paul 21/02/14 Aliquam.fringilla@dolordapibus.co.uk 4241033422900360 6738851$14.39 Bullock 2/7/2014adipiscing.fringilla@lectusjusto 5178789953524240 3336538$498.31 Mcmahon 11/5/2013lobortis.ultrices@lacus 5389589582467450 7734302$92.44 Walters 25/09/13 consectetuer.cursus.et@sitamet 5157094536097720 7794007$206.60 Franco 17/06/13 et@disparturientmontes.ca 345477952996264 2415873$89.12'))

更多推荐

当header = TRUE时,read.fwf出错

本文发布于:2023-11-15 16:55:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596275.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:TRUE   header   fwf   read

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!