为什么R为我的导入数据集名称添加前缀X.

时间:2013-04-02 01:15:54

标签: r csv import-from-csv

我不明白为什么标题名称会得到“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"            ...

我之所以引用=“”的原因是我的记录被截断,大概是因为埋在我的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"

1 个答案:

答案 0 :(得分:7)

列名称在返回之前通过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 =',')