meaning of ddply error: 'names' attribute [9] must be the same length as the vector [1]

坚强是说给别人听的谎言 提交于 2019-11-26 17:49:20

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
user1317221_G

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("removed@removed.ca", "removed@removed.net"),
                 B=c("please help a newbie compile mplayer :-)", 
                     "re: please help a newbie compile mplayer :-)"))

tapply(x2$B,x2$A,length)
removed@removed.ca removed@removed.net 
              1                   1 

ddply(x2,.(A),summarise, Freq = length(B))
                    A Freq
1  removed@removed.ca    1
2 removed@removed.net    1

you could also try more simply:

table(x2$A)

 removed@removed.ca removed@removed.net 
              1                   1 

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.

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)
Chuck

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...

Vanessa

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)

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!