计算变量的数量,将其分组并在r中求和

时间:2016-03-09 17:32:18

标签: r

我想根据类型计算列中不同变量的数量,并在另一列中计算类似变量长度的总和。数据看起来像

LEGEND  LENGTH  PCI
Existing Bike Path  14991   NO
Existing Bike Path  1116    NO
Planned Bike Route  9387    Yes
Planned Bike Route  4656    Yes
Planned Bike Route  4186    Yes
Planned Bike Route  4186    Yes
Planned Bike Route  4186    Yes
Planned Bike Route  4186    Yes
Existing Bike Path  5000    Yes

我希望输出像

Legend  count   sum of length
Existing Bike Path  3   21107
Planned Bike Route  6   30787

我能够计算变量的数量,但不知道如何添加长度并制作另一列。

library(plyr)
library(foreign)
a1=read.dbf("OBJECTID_1.dbf",as.is=FALSE)
z1= t(  count(a1    ,"  a1$LEGEND   ")  )
write.csv   (   z1, file=   "   object1 .csv"   )

请帮忙!!! 里拉

2 个答案:

答案 0 :(得分:1)

我建议使用dplyr包......

library(dplyr)

# make your data frame
legend <- c('Existing Bike Path','Existing Bike Path', 'Planned Bike Route', 'Planned Bike Route','Planned Bike Route', 'Planned Bike Route', 'Planned Bike Route','Planned Bike Route', 'Existing Bike Path')
length <- c(14991, 1116, 9387, 4656, 4186, 4186, 4186, 4186, 5000)
pci <- c('NO', 'NO', 'YES' , 'YES', 'YES', 'YES', 'YES', 'YES', 'YES')
data <- data.frame(legend, length, pci)

# funnel data through group_by and summarize to get the desired count and sum
data %>% group_by(legend) %>% summarize(count = n(), sum_o_length=sum(length))

您的输出看起来像这样......

              legend     count     sum_o_length
              (fctr)     (int)            (dbl)
1 Existing Bike Path        3            21107
2 Planned Bike Route        6            30787

为了写入csv ...

# store output as data frame
output <- data %>% group_by(legend) %>% summarize(count = n(), sum_of_length=sum(len))
write.table(output, file='output.csv', sep=',', row.names = FALSE)

答案 1 :(得分:0)

你可以尝试

aggregate(LENGTH ~ LEGEND, data, function(x) {c(no=length(x), sum = sum(x))})

#              LEGEND LENGTH.no LENGTH.sum
#1 Existing_Bike_Path         3      21107
#2 Planned_Bike_Route         6      30787

出于某种原因,如果你想使用plyr,那么这里有一个选项

library(plyr)
ddply(data, .(LEGEND), summarize, no = length(LENGTH), sum = sum(LENGTH))

#              LEGEND no   sum
#1 Existing_Bike_Path  3 21107
#2 Planned_Bike_Route  6 30787