read.table error

studentgrades.csv contains the below data.
StudentID, First, Last, Math, Science, Social Studies
011, Bob, Smith, 90, 80, 67
012, Jane, Weary, 75, , 80
010, Dan, "Thornton, III", 65, 75, 70
040, Mary, "O'Leary", 90, 95, 92

The code I am using is grades <- read.table("studentgrades.csv", header=TRUE, row.names="StudentID", sep=",") but I am getting the error Error in data[[rowvar]] : **
** attempt to select less than one element in get1index

Need some help to resolve this.

Why not just

library(tidyverse)
grades <- read_csv("studentgrades.csv")

The output comes as

ï..StudentID..First..Last..Math..Science..Social.Studies

011, Bob, Smith, 90, 80, 67
012, Jane, Weary, 75, , 80
010, Dan, "Thornton, III", 65, 75, 70
040, Mary, "O'Leary", 90, 95, 92

Also, I was trying to understand why this error is happening since this is from a book I am referring to.

I can't replicate your issue with the sample data you are providing, maybe this is related to the encoding of your file, can you provide a link to a sample file that reproduces your issue?

This makes no sense though I do not see why it throws an error. Oh wait, does a rowname need an entry in the header?

Using base read.csv("studentgrades.csv")works for me as does
dat1 <- read.table("studentgrades.csv", sep = ",")

BTW, if you do not need that , in "Thornton, III" you do not need the "" on "Thornton III" & "O'Leary"

When I use read.csv("studentgrades.csv"), I get the below.

ï..StudentID..First..Last..Math..Science..Social.Studies

011, Bob, Smith, 90, 80, 67
012, Jane, Weary, 75, , 80
010, Dan, "Thornton, III", 65, 75, 70
040, Mary, "O'Leary", 90, 95, 92

I am unable to understand why the character in bold appears in the title (i).

Have you tried

 read.table("studentgrades.csv", sep = ",")

what do you see when you try

readLines("studentgrades.csv")

?

The output comes as..

[1] "\"StudentID, First, Last, Math, Science, Social Studies\""
[2] "\"011, Bob, Smith, 90, 80, 67\""                             
[3] "\"012, Jane, Weary, 75, , 80\""                              
[4] "\"010, Dan, \"\"Thornton, III\"\", 65, 75, 70\""             
[5] "\"040, Mary, \"\"O'Leary\"\", 90, 95, 92\""

The csv is malformed.
What is the story of how the csv was generated ? that process should be amended.

library(readr)

malformedtxt <- c("\"StudentID, First, Last, Math, Science, Social Studies\"",
                  "\"011, Bob, Smith, 90, 80, 67\""                             ,
                  "\"012, Jane, Weary, 75, , 80\""                              ,
                  "\"010, Dan, \"\"Thornton, III\"\", 65, 75, 70\""             ,
                  "\"040, Mary, \"\"O'Leary\"\", 90, 95, 92\"")

tf <- tempfile()
writeLines(malformedtxt,tf)
(r1 <- readLines(tf))
cat(paste0(r1,"\n"))
(df_bad <- readr::read_csv(tf))


workingtxt<-c("StudentID, First, Last, Math, Science, \"Social Studies\"",
 "011, Bob, Smith, 90, 80, 67", 
 "012, Jane, Weary, 75, , 80" , 
 "010, Dan, \"Thornton, III\", 65, 75, 70", 
 "040, Mary,\"O'Leary\", 90, 95, 92")

tf <- tempfile()
writeLines(workingtxt,tf)
(r1 <- readLines(tf))
cat(paste0(r1,"\n"))
(df_good <- readr::read_csv(tf))
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.