📜  R编程语言代码示例中按组的行数

📅  最后修改于: 2022-03-11 14:51:59.154000             🧑  作者: Mango

代码示例1
# dplyr package does this with count/tally commands, or the n() function:
# First, some data:

df <- data.frame(x = rep(1:6, rep(c(1, 2, 3), 2)), year = 1993:2004, month = c(1, 1:11))

# Now the count:
library(dplyr)
count(df, year, month)

# piping
df %>% count(year, month)

# We can also use a slightly longer version with piping and the n() function:
df %>% 
  group_by(year, month) %>%
  summarise(number = n())
  
 dplyr package does this with count/tally commands, or the n() function:

# First, some data:
df <- data.frame(x = rep(1:6, rep(c(1, 2, 3), 2)), year = 1993:2004, month = c(1, 1:11))

# Now the count:
library(dplyr)
count(df, year, month)
# piping
df %>% count(year, month)

# We can also use a slightly longer version with piping and the n() function:

df %>% 
  group_by(year, month) %>%
  summarise(number = n())

# or the tally function:
df %>% 
  group_by(year, month) %>%
  tally()