8

我有一个数据框,比如工资单,比如:

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)

“NumRate”实际上是数字:

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

我想通过 PayBasis 了解最高、最低和平均工资。我希望这能奏效:

ddply(payroll, "PayBasis", summarize)

但相反,我收到了一个错误:Error: length(rows) == 1 is not TRUE

我在这里想念什么?

4

3 回答 3

4

可能是因为您误认为summarizesummary在这种情况下不会像您期望的那样工作)。你可能想要:

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

请务必更仔细地查看?summarize和中的示例?ddply

于 2013-06-11T21:11:46.757 回答
1

也可以是plyr图书馆。尽量不要加载它,或者,如果您需要它,请注意是否在其他库之前或之后加载它dplyr,或者tidyverse自动启动dplyr

于 2019-03-10T21:36:20.290 回答
0

确保您使用正确的命令使用dplyr::summarize. 你可以猜到有很多库使用“summarizes”命令

于 2021-08-20T21:21:13.373 回答