unique函数如何仅返回dataframe中的唯一值?
我有dataframe,我想根据容器类型过滤每个站点在给定时间的容器数量。使用的数据包括放行和接收日期、到达站、唯一集装箱代码和集装箱类型。数据帧的一行是一个移动unique函数如何仅返回dataframe中的唯一值?,r,dataframe,R,Dataframe,我有dataframe,我想根据容器类型过滤每个站点在给定时间的容器数量。使用的数据包括放行和接收日期、到达站、唯一集装箱代码和集装箱类型。数据帧的一行是一个移动 library(dplyr) library(tidyverse) library(lubridate) release.date <- as_date(c("2017-12-15","2018-05-31", "2017-03-03", "2017-11-16", "2017-10-15", "2017-12-22", "2
library(dplyr)
library(tidyverse)
library(lubridate)
release.date <- as_date(c("2017-12-15","2018-05-31", "2017-03-03", "2017-11-16", "2017-10-15", "2017-12-22", "2017-12-02"))
receive.date <- as_date(c("2018-12-15","2018-08-31", "2017-09-03", "2017-12-16", "2017-11-15", "2017-12-24", "2017-12-02"))
Routen2 <- data.frame(release.date, receive.date, destination.station = c("New York", "London", "New York", "London", "New York", "New York", "New York"), container.code = c("A1", "B2", "B1", "B1", "B4", "B3", "A1"), container.type = c("a", "b", "b", "b", "b", "b", "a"))
在这个样本数据中,结果完全缺乏伦敦。container.code在unique函数之后不是唯一的(当脚本在没有最后两行的情况下运行时)。为什么不起作用?不使用
基本R
函数(顺序
,唯一
),而使用tidyverse
函数(排列
,不同
)可以在链中取代它。此外,过滤器可以接受多个参数
library(tidyverse)
Routen2 %>%
filter(release.date <= "2017-12-31", receive.date <= "2017-12-31") %>%
arrange(desc(release.date)) %>%
distinct(container.code, .keep_all = TRUE) %>%
count(destination.station, container.type)
# A tibble: 3 x 3
# destination.station container.type n
# <fct> <fct> <int>
#1 London b 1
#2 New York a 1
#3 New York b 2
@谢谢,我在你的函数中添加了这个问题
destination.station container.type n
1 New York a 1
2 New York b 2
3 London b 1
library(tidyverse)
Routen2 %>%
filter(release.date <= "2017-12-31", receive.date <= "2017-12-31") %>%
arrange(desc(release.date)) %>%
distinct(container.code, .keep_all = TRUE) %>%
count(destination.station, container.type)
# A tibble: 3 x 3
# destination.station container.type n
# <fct> <fct> <int>
#1 London b 1
#2 New York a 1
#3 New York b 2
Routen2 %>%
filter(release.date <= "2017-12-31", receive.date <= "2017-12-31")%>%
.[order(.$release.date),] %>%
.[!duplicated(.$container.code, fromLast = TRUE), ]