39

I'm going through Machine Learning for Hackers, and I am stuck at this line:

from.weight <- ddply(priority.train, .(From.EMail), summarise, Freq = length(Subject))

Which generates the following error:

Error in attributes(out) <- attributes(col) : 
  'names' attribute [9] must be the same length as the vector [1]

This is a traceback():

> traceback()
11: FUN(1:5[[1L]], ...)
10: lapply(seq_len(n), extract_col_rows, df = x, i = i)
9: extract_rows(x$data, x$index[[i]])
8: `[[.indexed_df`(pieces, i)
7: pieces[[i]]
6: function (i) 
   {
       piece <- pieces[[i]]
       if (.inform) {
           res <- try(.fun(piece, ...))
           if (inherits(res, "try-error")) {
               piece <- paste(capture.output(print(piece)), collapse = "\n")
               stop("with piece ", i, ": \n", piece, call. = FALSE)
           }
       }
       else {
           res <- .fun(piece, ...)
       }
       progress$step()
       res
   }(1L)
5: .Call("loop_apply", as.integer(n), f, env)
4: loop_apply(n, do.ply)
3: llply(.data = .data, .fun = .fun, ..., .progress = .progress, 
       .inform = .inform, .parallel = .parallel, .paropts = .paropts)
2: ldply(.data = pieces, .fun = .fun, ..., .progress = .progress, 
       .inform = .inform, .parallel = .parallel, .paropts = .paropts)
1: ddply(priority.train, .(From.EMail), summarise, Freq = length(Subject))

The priority.train object is a data frame, and here is more info:

> mode(priority.train)
[1] "list"
> names(priority.train)
[1] "Date"       "From.EMail" "Subject"    "Message"    "Path"      
> sapply(priority.train, mode)
       Date  From.EMail     Subject     Message        Path 
     "list" "character" "character" "character" "character" 
> sapply(priority.train, class)
$Date
[1] "POSIXlt" "POSIXt" 

$From.EMail
[1] "character"

$Subject
[1] "character"

$Message
[1] "character"

$Path
[1] "character"

> length(priority.train)
[1] 5
> nrow(priority.train)
[1] 1250
> ncol(priority.train)
[1] 5
> str(priority.train)
'data.frame':   1250 obs. of  5 variables:
 $ Date      : POSIXlt, format: "2002-01-31 22:44:14" "2002-02-01 00:53:41" "2002-02-01 02:01:44" "2002-02-01 10:29:23" ...
 $ From.EMail: chr  "[email protected]" "[email protected]" "[email protected]" "[email protected]" ...
 $ Subject   : chr  "please help a newbie compile mplayer :-)" "re: please help a newbie compile mplayer :-)" "re: please help a newbie compile mplayer :-)" "re: please help a newbie compile mplayer :-)" ...
 $ Message   : chr  "    \n Hello,\n   \n         I just installed redhat 7.2 and I think I have everything \nworking properly.  Anyway I want to in"| __truncated__ "Make sure you rebuild as root and you're in the directory that you\ndownloaded the file.  Also it might complain of a few depen"| __truncated__ "Lance wrote:\n\n>Make sure you rebuild as root and you're in the directory that you\n>downloaded the file.  Also it might compl"| __truncated__ "Once upon a time, rob wrote :\n\n>  I dl'd gcc3 and libgcc3, but I still get the same error message when I \n> try rpm --rebuil"| __truncated__ ...
 $ Path      : chr  "../03-Classification/data/easy_ham/01061.6610124afa2a5844d41951439d1c1068" "../03-Classification/data/easy_ham/01062.ef7955b391f9b161f3f2106c8cda5edb" "../03-Classification/data/easy_ham/01063.ad3449bd2890a29828ac3978ca8c02ab" "../03-Classification/data/easy_ham/01064.9f4fc60b4e27bba3561e322c82d5f7ff" ...
Warning messages:
1: In encodeString(object, quote = "\"", na.encode = FALSE) :
  it is not known that wchar_t is Unicode on this platform
2: In encodeString(object, quote = "\"", na.encode = FALSE) :
  it is not known that wchar_t is Unicode on this platform

I would post a sample, but the content is a bit long and I don't think the content is relevant here.

The same error also happens here:

> ddply(priority.train, .(Subject))
Error in attributes(out) <- attributes(col) : 
  'names' attribute [9] must be the same length as the vector [1]

Does anyone have a clue on what's going on here? The error seems to be generated by a different object than priority.train, because its names attribute apparently has 9 elements.

I'd appreciate any help. Thanks!

Problem solved

I've found the problem thanks to @user1317221_G's tip of using the dput function. The problem is with the Date field, which is at this point a list that contains 9 fields (sec, min, hour, mday, mon, year, wday, yday, isdst). To solve the problem I've simply converted the dates into character vectors, used ddply then converted the dates back to Date:

> tmp <- priority.train$Date
> priority.train$Date <- as.character(priority.train$Date)
> from.weight <- ddply(priority.train, .(From.EMail), summarise, Freq = length(Subject))
> priority.train$Date <- tmp
> rm(tmp)
6
  • 4
    In the place of your additional information, may I suggest str(priority.train)? Jan 4, 2013 at 7:48
  • @sebastian-c Sure! I'll edit the question now.
    – mota
    Jan 4, 2013 at 8:15
  • "What does this error mean in R?" is probably the most useless question title you could have used. Please give it a little more thought next time.
    – flodel
    Jan 5, 2013 at 12:32
  • 15
    Use POSIXct dates in data.frames, not POSIXlt.
    – hadley
    Jan 8, 2013 at 20:25
  • 1
    I'm getting the same error on a date field. @hadley 's comment solved my problem. No surprise there.
    – Hack-R
    Jan 15, 2015 at 15:07

8 Answers 8

44

I fixed this problem I was having by converting format from POSIXlt to POSIXct as Hadley suggests above - one line of code:

    mydata$datetime<-strptime(mydata$datetime, "%Y-%m-%d %H:%M:%S") # original conversion from datetime string : > class(mydata$datetime) [1] "POSIXlt" "POSIXt" 
    mydata$datetime<-as.POSIXct(mydata$datetime) # convert to POSIXct to use in data frames / ddply
1
  • 8
    That single line 'mydata$datetime<-as.POSIXct(mydata$datetime) # convert to POSIXct to use in data frames / ddply' saved me from a circle of hell. Cheers
    – lamecicle
    Oct 20, 2014 at 12:17
6

You have probably already seen this and it has not helped. I guess we probably do not have an answer yet because people cannot reproduce your error.

A dput or smaller head(dput()) might help this. But here is an alternative using base:

x <- data.frame(A=c("a","b","c","a"),B=c("e","d","d","d"))

ddply(x,.(A),summarise, Freq = length(B))
  A Freq
1 a    2
2 b    1
3 c    1

 tapply(x$B,x$A,length)
a b c 
2 1 1 

Does this tapply work for you?

x2 <- data.frame(A=c("[email protected]", "[email protected]"),
                 B=c("please help a newbie compile mplayer :-)", 
                     "re: please help a newbie compile mplayer :-)"))

tapply(x2$B,x2$A,length)
[email protected] [email protected] 
              1                   1 

ddply(x2,.(A),summarise, Freq = length(B))
                    A Freq
1  [email protected]    1
2 [email protected]    1

you could also try more simply:

table(x2$A)

 [email protected] [email protected] 
              1                   1 
1
  • 1
    The examples you've written work fine. I've removed all the rows from the DF except the first two and I've set all values to NA. and then I ran the dput function that you've mentioned, and surprise! The Date field is a list with 9 fields (sec, min, hour, mday, mon, year, wday, yday, isdst). Converting the date field to a char vector solved the problem. Thank you!!
    – mota
    Jan 4, 2013 at 10:25
4

I had a very similar problem, although not sure if it is an identical one. I received the error below.

Error in attributes(out) <- attributes(col) : 
  'names' attribute [20388] must be the same length as the vector [128]

I don't have any variable in list mode, so Mota's solution does not work on my situation. The way I sorted the problem is to remove plyr 1.8 and manually install plyr 1.7. The error then is gone. I've also tried to reinstall plyr 1.8 and replicated the problem.

HTH.

1
  • 1
    I also saw the same error, which was fixed using Yishin's method.
    – lokheart
    Jan 30, 2013 at 9:10
3

I faced a similar problem with ddply as well and have given the code/error below:

    test <- ddply(test, "catColumn", function(df) df[1:min(nrow(df), 3),])
    Error: 'names' attribute [11] must be the same length as the vector [2]

There were quite a few categorical variables in the dataframe 'test'.

Converting the categorical variables to character variables as follows made the ddply command work:

    test <- data.frame(lapply(test, as.character), stringsAsFactors=FALSE)
2

Once you understand that it is that one date column that is interfering you can also simply leave that column out when you run the command rather than converting it...

so

from.weight <- ddply(priority.train, .(From.EMail), summarise, Freq = length(Subject))

can become

from.weight <- ddply(priority.train[,c(1:7,9:10)], .(From.EMail), summarise, Freq = length(Subject))

if for example the POSIXlt date happens to be in column 8 of the dataframe. What's odd about the reported error is that it may have nothing to do with either what you are attemtping to group by or what you are seeking as output information...

1

I had the same problem when using ddply and fixed it with doBy

library(doBy) 
bylength = function(x){length(x)} 
newdt = bylength(X ~From.EMail + To.EMail, data = dt, FUN = bylength)
0

I have also face the same issue, i resolve it by only keeping the required data for ddply and converting filter variable and all the required Text variables to character by using as.character

it worked

0
0

Without the data I cannot test this, but try using dplyr instead of plyr. Something like this should return the expected output. You have to force it back to a data frame, because the dplyr output will be a tibble.

from.weight <- priority.train %>%
               group_by(From.EMail) %>%
               summarise(Freq = length(Subject)) %>%
               ungroup() %>%
               as.data.frame()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.