[R] More Columns than column names Error

arun smartpink111 at yahoo.com
Tue Oct 22 21:07:25 CEST 2013


The "Garbage.txt" file you showed in the original post is slighly different (in spacing) from the one you are showing now.

lines1 <- readLines("Garbage.txt",warn=FALSE)

lines2 <- readLines("GarbageNew.txt",warn=FALSE) #saved the new as "GarbageNew.txt"

 [1] "Material\tWeight(Million Tons)\tPercent"
 [2] "Food Scraps\t25.9\t\t\t11.2"            
 [3] "Glass\t\t12.8\t\t\t5.5"                 
 [4] "Metals\t\t18.0\t\t\t7.8"                
 [5] "Paper\t\t86.7\t\t\t37.4"                
 [6] "Plastics\t24.7\t\t\t10.7"               
 [7] "Textiles\t15.8\t\t\t6.8"                
 [8] "Wood\t\t12.7\t\t\t5.5"                  
 [9] "Yard trimmings\t27.7\t\t\t11.9"         
[10] "Other\t\t7.5\t\t\t3.2"                  
[11] "Total\t\t231.9\t\t\t100.0"              
 [1] "Material\tWeight(Million Tons)\tPercent"
 [2] "Food Scraps\t25.9\t11.2"                
 [3] "Glass\t12.8\t5.5"                       
 [4] "Metals\t18.0\t7.8"                      
 [5] "Paper\t86.7\t37.4"                      
 [6] "Plastics\t24.7\t10.7"                   
 [7] "Textiles\t15.8\t6.8"                    
 [8] "Wood\t12.7\t5.5"                        
 [9] "Yard trimmings\t27.7 11.9"   #####here there are two delimiters "\t" and "space"            
[10] "Other\t7.5\t3.2"                        
[11] "Total\t231.9\t100.0"      

Looks like you changed the tab spaces.
dat1 <- read.table(text=gsub("\t+","\t",lines1),stringsAsFactors=FALSE,sep="\t",check.names=FALSE,header=TRUE) #no errors

#able to reproduce the error mentioned

dat2 <- read.table(text=gsub("\t+","\t",lines2),stringsAsFactors=FALSE,sep="\t",check.names=FALSE,header=TRUE)
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 8 did not have 3 elements

#Try this
 dat2 <- read.table(text=gsub("(.*\\d+) (\\d+)","\\1\t\\2",lines2),stringsAsFactors=FALSE,sep="\t",check.names=FALSE,header=TRUE)
#[1] TRUE

 barplot(dat2[-10,2],names.arg=dat2[-10,1],ylab=colnames(dat2)[2],col=rainbow(9),main="American garbage")

Attached is the pdf of the figure


The error I get now is "Line 8 did not have 3 elements" 

On Tuesday, October 22, 2013 9:41 AM, arun <smartpink111 at yahoo.com> wrote:
 lines1 <- readLines("Garbage.txt",warn=FALSE)
dat1 <- read.table(text=gsub("\t+","\t",lines1),stringsAsFactors=FALSE,sep="\t",check.names=FALSE,header=TRUE)
#'data.frame':    10 obs. of  3 variables:
# $ Material            : chr  "Food Scraps" "Glass" "Metals" "Paper" ...
# $ Weight(Million Tons): num  25.9 12.8 18 86.7 24.7 ...
# $ Percent             : num  11.2 5.5 7.8 37.4 10.7 6.8 5.5 11.9 3.2 100
 barplot(dat1[-10,2],names.arg=dat1[-10,1],ylab=colnames(dat1)[2],col=rainbow(9),main="American garbage")


Hello guys, I'm having this weird problem with my assignment. I can't seem to import the data that I have created. 

I keep getting an error that says "Error: More Columns than Column names" 

This is my data file. 

Was also wondering if you guys could send me into the right direction on how to do this. 

1. Create a data frame with the data given above. 
2. Create the bar plot for weight variable with appropriate labels. Resize the graphics 
panel/ window so that all labels are visible. 
3. Add colors. Suppose n is the number of bars. 
> barplot(..., col = rainbow(n)) 
4. Save the plot to garbageweight.jpg 

This is how it's supposed to look like at the end.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: garbage.weight.pdf
Type: application/pdf
Size: 4688 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20131022/67898a6f/attachment.pdf>

More information about the R-help mailing list