简体   繁体   English

group_by不总结

[英]group_by does not summarize

I am having a hard time working with the dplyr library. 我在使用dplyr库时遇到了困难。 I have been trying to implement a relatively easy piece of code but for some reason when I group by one variable and try to sum to get the total for that variable I get only NA values. 我一直在尝试实现一个相对简单的代码,但是由于某种原因,当我将一个变量分组并试图求和以获得该变量的总和时,我只会得到NA值。 Here are my files: 这是我的文件:

https://www.dropbox.com/sh/zhxfj6cm6gru0t1/AAA-DgeTrngJ0md12W2bEzi0a https://www.dropbox.com/sh/zhxfj6cm6gru0t1/AAA-DgeTrngJ0md12W2bEzi0a

And this is code: 这是代码:

library (dplyr)
#we set the working directory
setwd("~/asado/R/emp")
##we list the files
list.files()
##we load the csv files
emp1 <- read.csv("AI_EMP_CT_A.csv", sep=',')
##emp1 contains employment information for US counties with naics classification
##empva is another part of the same dataset
empva <- read.csv("AI_EMP_CT_VA_A.csv", sep=',')
##we merge our files, they have the same dimentions so rbind works
emp <- data.frame(rbind(emp1, empva))
##we create a variable to summarize our data
##and make sure is stored as character
emp$naics <- as.character(substring(emp$Mnemonic,3,6))

##we try to summarize by the variable naics, summing for Dec.2013
useemp<- emp%.% group_by(naics) %.%
  summarize(total=sum(Dec.2013, na.rm=T))
##the resulting dataframe shows NA
head(useemp)

Any idea what's going on? 知道发生了什么吗?

This works for me, but it was complicated to read your empva file because the last column, the Dec.2013 was filled of ; 这对我有用,但是读取您的empva文件非常复杂,因为最后一栏(2013年12月)已填充; and not separated from it. 并没有与之分离。 Are you sure it is read as numeric? 您确定将其读取为数字吗?

useemp <- emp %>% group_by(naics) %>%
      summarize(total=sum(Dec.2013, na.rm=T))
    head(useemp)

    Source: local data frame [6 x 2]

      naics     total
    1  2111 132.04674
    2  2121  24.84666
    3  2122  23.90470
    4  2123  17.57697
    5  2131  77.20557
    6  2211 119.30697

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM