【问题标题】:Why is R prefixing my imported dataset names with an X [duplicate]为什么 R 在我导入的数据集名称前加上 X [重复]
【发布时间】:2013-04-02 01:15:53
【问题描述】:

我不知道为什么标题名称会出现“X”。使用quote=""导入时的前缀。代码如下:

xhead = read.csv("~/Desktop/dbdump/users.txt", na.strings = "\\N", quote="", nrows = 1000)

这给了我:

names(xhead)
 [1] "X.userId."             "X.fullName."           "X.email."              "X.password."          
 [5] "X.activated."          "X.registrationDate."   "X.locale."             ...

鉴于:

yhead = read.csv("~/Desktop/dbdump/users.txt", na.strings = "\\N", nrows = 1000)
names(yhead)
 [1] "userId"             "fullName"           "email"              "password"          
 [5] "activated"          "registrationDate"   "locale"            ...

我有 quote="" 的原因是我被截断了记录,大概是因为在我的 15000 条记录中隐藏了一个杂散的引号。

这是我的数据文件的样子:

"userId", "fullName","email","password","activated","registrationDate","locale","notifyOnUpdates","lastSyncTime","plan_id","plan_period_months","plan_price","plan_exp_date","plan_is_trial","plan_is_trial_used","q_hear","q_occupation","pp_subid","pp_payments","pp_since","pp_cancelled","apikey"
"2","Adam Smith","a@mail.com","*****","1","2004-07-23 14:19:32","en_US","1","2011-04-07 07:29:17","3",\N,\N,\N,"0","1",\N,\N,\N,\N,\N,\N,"d7734dce-4ae2-102a-8951-0040ca38ff83"

【问题讨论】:

  • doesn't read.csv 暗示 header=TRUE?
  • 你是对的。删除了我的评论

标签: r csv import-from-csv


【解决方案1】:

在返回之前,列名通过make.names 运行。引号不是列名的有效字符。你可以通过运行看到区别:

make.names(c('"userId"', "fullName"))
[1] "X.userId." "fullName"

来自make.names 帮助:

语法上有效的名称由字母、数字和点或下划线字符组成,并以字母或点开头,后跟数字。 ... 如有必要,可在前面加上字符“X”。所有无效字符都被翻译成“.”。

建议调用read.csv 跳过第一行,不包括标题以获取大量数据。

dd <- read.csv("~/Desktop/dbdump/users.txt", na.strings = "\\N", 
         quote="", nrows = 1000, header = FALSE, skip = 1)

然后您可以使用 scan 读取列名(这是 read.csv 在后台调用的内容)

names(dd) <- scan("~/Desktop/dbdump/users.txt", what = character(), nlines=1,sep =',')

【讨论】:

    猜你喜欢
    • 2012-09-26
    • 2010-11-05
    • 2011-12-02
    • 2017-06-07
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 2012-01-04
    相关资源
    最近更新 更多