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

Tyler Rinker tyler_rinker at hotmail.com
Mon May 14 22:36:05 CEST 2012



I'd throw a browser() in at that point and see what colnames(newdf.int) gives you.  If you have less columns than names this is likely the reason for the error.
You can get the same error with:

colnames(mtcars) <- LETTERS

Cheers,Tyler
----------------------------------------
> Date: Mon, 14 May 2012 11:35:07 -0700
> From: bhattp60 at gmail.com
> To: r-help at r-project.org
> Subject: [R] Error in names(x) <- value: 'names' attribute must be the same length as the vector
>
> 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!
>
> 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.
 		 	   		  


More information about the R-help mailing list