是否有 R 函数显示数据集中特定列中每个唯一出现的频率?
Is there a R function which displays the frequency of each of unique occurrence in a specific column in a dataset?
为了简单起见,我创建了小型虚拟数据集。
library(tidyverse)
library(lubridate)
myDF <- tibble(country = rep(c("UK", "US"), each = 3),
date = c("2020-01-01", "2020-02-01", "2020-02-01", "2020-03-01",
"2020-03-01", "2020-03-01"))
myDF <- myDF %>% mutate(date = as_date(date))
country date
<chr> <date>
1 UK 2020-01-01
2 UK 2020-02-01
3 UK 2020-02-01
4 US 2020-03-01
5 US 2020-03-01
6 US 2020-03-01
我知道 unique() 函数可用于查找在日期列。
unique(myDF$date) # the unique values
length(unique(myDF$date)) # number of unique values
但是我如何创建一个小的 table 输出来显示数据集中特定列(即日期)中每个唯一出现的频率?我正在寻找这样的东西:
myDF$date freq
"2020-01-01" 1
"2020-02-01" 2
"2020-03-01" 3
You can do something like
library(dplyr)
myDF %>% count(date, name = 'freq')
为了简单起见,我创建了小型虚拟数据集。
library(tidyverse)
library(lubridate)
myDF <- tibble(country = rep(c("UK", "US"), each = 3),
date = c("2020-01-01", "2020-02-01", "2020-02-01", "2020-03-01",
"2020-03-01", "2020-03-01"))
myDF <- myDF %>% mutate(date = as_date(date))
country date
<chr> <date>
1 UK 2020-01-01
2 UK 2020-02-01
3 UK 2020-02-01
4 US 2020-03-01
5 US 2020-03-01
6 US 2020-03-01
我知道 unique() 函数可用于查找在日期列。
unique(myDF$date) # the unique values
length(unique(myDF$date)) # number of unique values
但是我如何创建一个小的 table 输出来显示数据集中特定列(即日期)中每个唯一出现的频率?我正在寻找这样的东西:
myDF$date freq
"2020-01-01" 1
"2020-02-01" 2
"2020-03-01" 3
You can do something like
library(dplyr)
myDF %>% count(date, name = 'freq')