在R中创建一个条件计数变量

时间:2016-09-26 12:37:36

标签: r database dplyr

我想创建一个计数变量,其中每个给定年份的Z == 0的人数。如下图所示:

PersonID    Year    Z   Count*
       1    1990    0   1
       2    1990    1   1
       3    1990    1   1
       4    1990    2   1
       5    1990    1   1
       1    1991    1   3
       2    1991    0   3
       3    1991    1   3
       4    1991    0   3
       5    1991    0   3
       1    1992    NA  1
       2    1992    2   1
       3    1992    2   1
       4    1992    0   1
       5    1993    1   0
       1    1993    1   0
       2    1993    2   0
       3    1993    NA  0
       4    1993    1   0
       5    1994    0   5
       1    1994    0   5
       2    1994    0   5
       3    1994    0   5
       4    1994    0   5

我看了之前的R-scripts并发现了这个

library(dplyr)
sum_data <- data %>% group_by(PersonID) %>% summarise(Count = sum(Z, na.rm=T))

有人可以帮助我做到这一点吗? count变量基本上应该计算Z == 0的总人数,格式与我上面说明的相同。谢谢!

dput(data)
structure(list(PersonID = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L), 
    Year = c(1990L, 1990L, 1990L, 1990L, 1990L, 1991L, 1991L, 
    1991L, 1991L, 1991L, 1992L, 1992L, 1992L, 1992L, 1993L, 1993L, 
    1993L, 1993L, 1993L, 1994L, 1994L, 1994L, 1994L, 1994L), 
    Z = c(0L, 1L, 1L, 2L, 1L, 1L, 0L, 1L, 0L, 0L, NA, 2L, 2L, 
    0L, 1L, 1L, 2L, NA, 1L, 0L, 0L, 0L, 0L, 0L)), .Names = c("PersonID", 
"Year", "Z"), class = "data.frame", row.names = c(NA, -24L))

2 个答案:

答案 0 :(得分:1)

这是一个简单的解决方案:

library(dplyr)

sum_data <- df %>%
  mutate(Z=replace(Z, is.na(Z), 1)) %>%
  mutate(temp = ifelse(Z == 0, 1, 0)) %>%
  group_by(Year) %>%
  summarize(count = sum(temp))

基本上这就是代码正在做的事情:

  • mutate(Z=replace(Z, is.na(Z), 1))将NA替换为1(可选)
  • mutate(temp = ifelse(Z == 0, 1, 0))创建条件临时值 变量:
  • ifelse(Z == 0, 1, 0)说如果Z == 0则值为1 否则0
  • group_by(Year)非常详尽:)它将数据框分组 年
  • summarize(count = sum(temp))创建一个count变量 早期生成的temp的总和

结果:

   Year count
  <int> <int>
1  1990     5
2  1991     5
3  1992     4
4  1993     5
5  1994     5

如果您想将此数据加入原始数据框,只需使用join:

left_join(df, sum_data)
Joining, by = "Year"
   PersonID Year  Z count
1         1 1990  0     1
2         2 1990  1     1
3         3 1990  1     1
4         4 1990  2     1
5         5 1990  1     1
6         1 1991  1     3
7         2 1991  0     3
8         3 1991  1     3
9         4 1991  0     3
10        5 1991  0     3
11        1 1992 NA     1
12        2 1992  2     1
13        3 1992  2     1
14        4 1992  0     1
15        5 1993  1     0
16        1 1993  1     0
17        2 1993  2     0
18        3 1993 NA     0
19        4 1993  1     0
20        5 1994  0     5
21        1 1994  0     5
22        2 1994  0     5
23        3 1994  0     5
24        4 1994  0     5

答案 1 :(得分:1)

试试这个:

library(dplyr)
df <- left_join(data, data %>% filter(Z==0) %>% group_by(Year) %>% summarise(Count = n())) 
df[is.na(df$Count),]$Count <- 0


 PersonID Year  Z Count
1         1 1990  0     1
2         2 1990  1     1
3         3 1990  1     1
4         4 1990  2     1
5         5 1990  1     1
6         1 1991  1     3
7         2 1991  0     3
8         3 1991  1     3
9         4 1991  0     3
10        5 1991  0     3
11        1 1992 NA     1
12        2 1992  2     1
13        3 1992  2     1
14        4 1992  0     1
15        5 1993  1     0
16        1 1993  1     0
17        2 1993  2     0
18        3 1993 NA     0
19        4 1993  1     0
20        5 1994  0     5
21        1 1994  0     5
22        2 1994  0     5
23        3 1994  0     5
24        4 1994  0     5