[R] Error in names(x) <- value: 'names' attribute must be the same length as the vector

David Winsemius dwinsemius at comcast.net
Tue May 15 00:13:59 CEST 2012


On May 14, 2012, at 2:35 PM, Priya Bhatt wrote:

> Dear R-helpers,
>
> I am stuck on an error in R:  When I run my code (below), I get this  
> error
> back:
>
> Error in names(x) <- value :
>  'names' attribute must be the same length as the vector
>
>
> Then when I use traceback(), R gives me back this in return:
>
> `colnames<-`(`*tmp*`, value = c(""Item", "Color" ,"Number", "Size"))
>
>
>
> I'm not exactly sure how to fix this problem.  Any advice would be  
> greatly
> appreciated!

Why not throw in some print statements to figure out which file is  
causing trouble? ( And I would suggest not using "c" as a loop  
variable.)

Then you use str() to look at 'currentCSVFile' or  'newdf.int' after  
isolating the source of problem.

-- 
David.
>
> Thanks,
> Priya
>
>
> MODIFIED CODE:
> # Looping through a series of CSV files
> for (c in csvfiles)
> {
>  #A DF (prevdf) was created based on an initial csv file..
>  #so the condition below states that if there are rows with NAs or the
> number of rows in prevdf is zero
>  if( (apply(prevdf, 1, function(y) !sum(!is.na(y))==1) > 0) ||
> (nrow(prevdf) == 0) )
>  {
>    #Open a new file
>    currentCSVFile <- read.csv(c, header=TRUE)
>    #pick only the few columns we want from the file
>    currentCSVFile <- data.frame(currentCSVFile$Item,
> currentCSVFile$Color..type , currentCSVFile$Number..owned,
> currentCSVFile$Size..shirt)
>    #rename the column names
>    colnames(currentCSVFile) <- c("Item", "Color" ,"Number", "Size")
>
>    #find the rows in prevdf that do not have any values. (sum should  
> be 1
> because the Item name is unique for every row)
>    NArows <- prevdf[apply(prevdf, 1, function(y) sum(!is.na(y))==1),]
>
>    #if NAs rows is not equal to zero
>    if (nrow(NArows) != 0 )
>    {
>      #find the rows in the current CSV file where there is missing  
> data in
> prevdf (this info is in NArows)
>      intersectItem<- intersect(currentCSVFile$Item, NArows$Item)
>
>      #initiate another data frame to put the data in
>      newdf.int <- data.frame(Item=c(), Color=c(), Number=c(),  
> Size=c())
>
>
>      print(nrow(currentCSVFile))
>      for (i in 1:nrow(currentCSVFile))
>
>      {
>        print("In loop") # check for me
>        row <- currentCSVFile[i,]
>
>        if (row$Item %in% intersectItem){  # this is where the code  
> stops
> and throws back error
>          .
>          .
>          .
>           # do stuff to fill vectors named Item, Color, Number and  
> Size
>          .
>          .
>          .
>
>          newdf.int <-rbind(newdf.int, c(Item, Color, Number, Size)
>        }
>
>        colnames(newdf.int) <- c("Item", "Color", "Number", "Size")
>        prevdf <- merge(newdf.int, prevdf, by=c("Item", "Color",  
> "Number",
> "Size"), all=TRUE)
>        prevdf <- prevdf[apply(prevdf, 1, function(y) !sum(! 
> is.na(y))==1),]
>        print("after removing row = 1")
>
>
>      } # end of for loop
>
>    } # end of NA rows condition
>
>  } # end of main if statement
>
>  else
>  {
>    break
>  }
>
>
> }
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list