Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I seeing "Error: length(rows) == 1 is not TRUE" with ddply?

Tags:

r

plyr

summary

I have a data frame, say payroll, like:

payroll <- read.table(text="
AgencyName          Rate             PayBasis     Status    NumRate
HousingAuthority    $26,843.00   Annual           Full-Time 26843.00
HousingAuthority    $14,970.00   ProratedAnnual   Part-Time 14970.00
HousingAuthority    $26,843.00   Annual           Full-Time 26843.00
HousingAuthority    $14,970.00   ProratedAnnual   Part-Time 14970.00
HousingAuthority    $13.50           Hourly           Part-Time 13.50
HousingAuthority    $14,970.00   ProratedAnnual   Part-Time 14970.00
HousingAuthority    $26,843.00   Annual           Full-Time 26843.00", header = TRUE)

The "NumRate" is actually numeric:

payroll$NumRate <- as.numeric(payroll$NumRate)

And I'd like to get a know the max, min and mean salaries by PayBasis. I expect this to work:

ddply(payroll, "PayBasis", summarize)

But instead I'm getting an error: Error: length(rows) == 1 is not TRUE

What am I missing here?

like image 709
Amanda Avatar asked Jun 11 '13 21:06

Amanda


3 Answers

Probably because you've mistaken summarize for summary (which won't work like you expect in this context). You probably wanted:

ddply(payroll, "PayBasis", summarize,mx = max(NumRate),mn = min(NumRate),avg = mean(NumRate))
        PayBasis      mx      mn     avg
1         Annual 26843.0 26843.0 26843.0
2         Hourly    13.5    13.5    13.5
3 ProratedAnnual 14970.0 14970.0 14970.0

And be sure to look more carefully at the examples in ?summarize and ?ddply.

like image 155
joran Avatar answered Oct 04 '22 22:10

joran


It could also be the plyr library. Try either not to load it, or, if you need it be careful whether you load it before or after other libraries such as dplyr, or tidyverse which automatically fires up the dplyr

like image 34
Hossein Noorazar Avatar answered Oct 04 '22 20:10

Hossein Noorazar


to make sure you are using the correct command use dplyr::summarize. As you can guess there are a lot of libraries that uses "summarizes" command

like image 41
user16716706 Avatar answered Oct 04 '22 21:10

user16716706