楼主: yearbattle
3539 7

[问答] 如何读入数据不在同一行的txt文件? [推广有奖]

  • 0关注
  • 0粉丝

博士生

4%

还不是VIP/贵宾

-

威望
0
论坛币
344 个
通用积分
39.5158
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
207 点
帖子
33
精华
0
在线时间
393 小时
注册时间
2014-4-26
最后登录
2025-10-28

楼主
yearbattle 发表于 2016-2-5 11:44:26 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
The U.S. Gasoline Market, 52 Yearly Observations, 1953-2004
Source: These data were compiled by Professor Chris Bell, Department of Economics, University of North Carolina, Asheville. Sources: www.bea.gov and www.bls.gov

    * Year = Year, 1953-2004,
    * GasExp = Total U.S. gasoline expenditure,
    * Pop = U.S. total population in thousands
    * GasP = Price index for gasoline,
    * Income = Per capita disposable income,
    * Pnc = Price index for new cars,
    * Puc = Price index for used cars,
    * Ppt = Price index for public transportation,
    * Pd = Aggregate price index for consumer durables,
    * Pn = Aggregate price index for consumer nondurables,
    * Ps = Aggregate price index for consumer services.



Year,GasExp,Pop,Gasp,Income,PNC,PUC,PPT,PD,PN,PS
   1953.00       7.40000       159565.       16.6680       8883.00   
   47.2000       26.7000       16.8000       37.7000       29.7000   
   19.4000   

以上为数据截取,
> read.table("C:/Users/forYear/Documents/R/工作/2014Homework1_datasets/Gasoline.txt", header = FALSE, sep = "", skip = 18)
>Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
  line 3 did not have 5 elements

因为11个变量,在txt里分为了三行排列,所以不能使用上述命令,应该怎么解决这一个问题

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:读入数据 txt observations Transportat expenditure 如何

回帖推荐

rosenbloog 发表于4楼  查看完整内容

rawlines

沙发
jiangbeilu 学生认证  发表于 2016-2-5 20:49:09
建议上传部分文件哈

藤椅
yearbattle 发表于 2016-2-6 11:37:20
jiangbeilu 发表于 2016-2-5 20:49
建议上传部分文件哈
Gasoline.txt (8.91 KB)

板凳
rosenbloog 发表于 2016-2-6 12:27:49
rawlines <- readLines(file(".../Gasoline.txt"))
rawlines2 = rawlines[19:length(rawlines)]

names = unlist(strsplit(rawlines[18], ","))

lines <- paste(rawlines2[c(TRUE, FALSE, FALSE)], rawlines2[c(FALSE, TRUE, FALSE)],
               rawlines2[c(FALSE, FALSE, TRUE)], collapse = "\n")
data <- read.table(text = lines, na.string = "*")

names(data) = names
已有 3 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
yearbattle + 5 + 5 + 5 精彩帖子
admin_kefu + 15 热心帮助其他会员
ryoeng + 1 热心帮助其他会员

总评分: 论坛币 + 15  学术水平 + 5  热心指数 + 6  信用等级 + 5   查看全部评分

报纸
ryoeng 在职认证  发表于 2016-2-6 16:03:04
提示: 作者被禁止或删除 内容自动屏蔽
签名被屏蔽

地板
liangwubo8 发表于 2016-3-29 09:37:10
使用 infile v1 v2 v3 "路径" 格式输入,希望能帮到你,可用help infile 查看
已有 1 人评分论坛币 热心指数 收起 理由
admin_kefu + 12 + 1 热心帮助其他会员

总评分: 论坛币 + 12  热心指数 + 1   查看全部评分

7
∶Re、朝鼬 发表于 2017-3-1 10:39:32
ryoeng 发表于 2016-2-6 16:03
你好,我暂时还没有论坛币,能给我传一下 Gasoline.txt 这个数据吗?
qq 584062295

8
ayzzya 发表于 2017-3-1 11:35:10
支持支持,学习了

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-8 20:37