简体   繁体   English

R read.table与read.csv

[英]R read.table vs read.csv

grades <- read.table("studentgrades.csv",header = TRUE,row.names="StudentID", sep = ",")

gradess <- read.csv("studentgrades.csv",header = TRUE,row.names="StudentID", sep = ",")

The result of read.table is: read.table的结果是:

grades
[1] First          Last           Math           Science        Social.Studies
<0 rows> (or 0-length row.names)

The result of read.csv is: read.csv的结果是:

gradess
   First       Last Math Science Social.Studies
11   Bob      Smith   90      80             67
12  Jane      Weary   75      NA             80
10   Dan "Thornton"   65      75             70
40  Mary    O'Leary   90      95             92

I just don't know why the read.tables can not give me the right result. 我只是不知道为什么read.tables不能给我正确的结果。

The problem is due to the quote (') in O'Leary of last name column. 问题是由于姓氏O'Leary列中的引号(')引起的。 You will need to change the default quote option in read.table which is set to the (') by default to get desired result. 您将需要更改read.table的默认quote选项,该选项默认情况下设置为(')以获取所需的结果。

If you use quote=NULL in read.table like below 如果您在read.table使用quote=NULL ,如下所示

grades <- read.table("studentgrades.csv",header = TRUE,sep=",",quote=NULL,row.names="StudentID")

Then you get the desired result. 然后,您将获得所需的结果。

> grades
    First        Last Math  Science Social.Studies
11    Bob       Smith   90       80             67
12   Jane       Weary   75       NA             80
10    Dan  "Thornton"   65       75             70
40   Mary     O'Leary   90       95             92

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM