问题
我有一个数据框,每行标记公司之间的交换,公司在给定日期给予和接收某些东西(他们可以给不同的公司或他们自己)。从那时起,我想创建一个新的数据框,其中的列指示公司何时首次开始给予,何时首次停止给予,何时首次开始接收,何时首次停止接收。这是我开始的示例数据框架:
示例起始数据
samp <- structure(list(giver = structure(c(1L, 2L, 6L, 3L, 1L, 3L, 4L, 1L, 6L, 1L, 5L), .Label = c("A", "B", "C", "X", "Y", "Z"), class = "factor"), receiver = structure(c(1L, 2L, 2L, 3L, 1L, 3L, 3L, 1L, 2L, 1L, 2L), .Label = c("A", "B", "C"), class = "factor"), date = structure(c(1L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 9L), .Label = c("2000-01-01", "2000-01-02", "2000-01-03", "2000-01-04", "2000-01-05", "2000-01-06", "2000-01-07", "2000-01-08", "2000-01-09"), class = "factor")), .Names = c("giver", "receiver", "date"), class = "data.frame", row.names = c(NA, -11L))
samp$date <- as.Date(samp$date, "%Y-%m-%d") # Format date variable
samp
giver receiver date
A A 2000-01-01
B B 2000-01-01
Z B 2000-01-02
C C 2000-01-03
A A 2000-01-04
C C 2000-01-05
X C 2000-01-06
A A 2000-01-07
Z B 2000-01-08
A A 2000-01-09
Y B 2000-01-09
但是,我无法确定如何扫描每个公司的第一个和最后一个列的一列,并返回不同列的日期值。我使用match
,duplicated
或tapply
发现了类似问题here和here,但无法让它们符合我的目标。以下是我希望最终得到的示例数据框架:
所需的结果数据
desire <- structure(list(company = structure(1:6, .Label = c("A", "B", "C", "X", "Y", "Z"), class = "factor"), start.giving = structure(c(1L, 1L, 3L, 4L, 5L, 2L), .Label = c("2000-01-01", "2000-01-02", "2000-01-03", "2000-01-05", "2000-01-09"), class = "factor"), stop.giving = structure(c(5L, 1L, 2L, 3L, 5L, 4L), .Label = c("2000-01-01", "2000-01-05", "2000-01-06", "2000-01-08", "2000-01-09"), class = "factor"), start.receiving = structure(c(1L, 1L, 2L, NA, NA, NA), .Label = c("2000-01-01", "2000-01-03"), class = "factor"), stop.receiving = structure(c(2L, 2L, 1L, NA, NA, NA), .Label = c("2000-01-06", "2000-01-09"), class = "factor")), .Names = c("company", "start.giving", "stop.giving", "start.receiving", "stop.receiving"), class = "data.frame", row.names = c(NA, -6L))
desire
company start.giving stop.giving start.receiving stop.receiving
A 2000-01-01 2000-01-09 2000-01-01 2000-01-09
B 2000-01-01 2000-01-01 2000-01-01 2000-01-09
C 2000-01-03 2000-01-05 2000-01-03 2000-01-06
X 2000-01-05 2000-01-06 <NA> <NA>
Y 2000-01-09 2000-01-09 <NA> <NA>
Z 2000-01-02 2000-01-08 <NA> <NA>
答案 0 :(得分:4)
dplyr
版本:
library("dplyr")
giving <- samp %>% group_by(giver) %>%
summarise(start.giving=min(date),
stop.giving=max(date)) %>%
rename(company=giver)
receiving <- samp %>% group_by(receiver) %>%
summarise(start.receiving=min(date),
stop.receiving=max(date)) %>%
rename(company=receiver)
full_join(giving,receiving)
通过更多的工作,可以进一步压缩这一点/不重复所有summarise
代码(类似于@ Arun的答案中的foo()
函数) ...
foo <- function(x,f) {
ss <- c("start","stop")
group_by_(x,.dots=f) %>%
summarise(start=min(date),
stop=max(date)) %>%
rename_(.dots=c(company=f,
setNames(ss,paste(ss,f,sep="."))))
}
full_join(foo(samp,"giver"),foo(samp,"receiver"))
...虽然代码现在不那么透明了,实际上没有任何更短......如果你要做很多这样的事情,那将是值得的。
答案 1 :(得分:3)
这是使用data.table
包
library(data.table)
setDT(samp)
Res1 <- samp[, .(start = min(date), stop = max(date)), by = .(company = giver)]
Res2 <- samp[, .(start = min(date), stop = max(date)), by = .(company = receiver)]
merge(Res1, Res2, by = "company", all = TRUE, suffixes = c(".giving", ".receiving"))
# company start.giving stop.giving start.receiving stop.receiving
# 1: A 2000-01-01 2000-01-09 2000-01-01 2000-01-09
# 2: B 2000-01-01 2000-01-01 2000-01-01 2000-01-09
# 3: C 2000-01-03 2000-01-05 2000-01-03 2000-01-06
# 4: X 2000-01-06 2000-01-06 <NA> <NA>
# 5: Y 2000-01-09 2000-01-09 <NA> <NA>
# 6: Z 2000-01-02 2000-01-08 <NA> <NA>
答案 2 :(得分:2)
Using devel version of data.table
, 1.9.5,这是使用dcast
新功能的另一个版本:
require(data.table) ## v1.9.5+
foo <- function(x, col) {
ans <- dcast(x, paste(col, "~ ."), value.var="date", fun=list(min, max))
setnames(ans, c("company", "start", "stop"))
}
setDT(samp)
merge(foo(samp, "giver"), foo(samp, "receiver"), by = "company",
all=TRUE, suffixes=c(".giving", ".receiving"))
# company start.giving stop.giving start.receiving stop.receiving
# 1: A 2000-01-01 2000-01-09 2000-01-01 2000-01-09
# 2: B 2000-01-01 2000-01-01 2000-01-01 2000-01-09
# 3: C 2000-01-03 2000-01-05 2000-01-03 2000-01-06
# 4: X 2000-01-06 2000-01-06 <NA> <NA>
# 5: Y 2000-01-09 2000-01-09 <NA> <NA>
# 6: Z 2000-01-02 2000-01-08 <NA> <NA>
答案 3 :(得分:1)
这可以使用aggregate
和merge
命令在基础R中完成:
# Import starting sample data
samp <- structure(list(giver = structure(c(1L, 2L, 6L, 3L, 1L, 3L, 4L, 1L, 6L, 1L, 5L), .Label = c("A", "B", "C", "X", "Y", "Z"), class = "factor"), receiver = structure(c(1L, 2L, 2L, 3L, 1L, 3L, 3L, 1L, 2L, 1L, 2L), .Label = c("A", "B", "C"), class = "factor"), date = structure(c(1L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 9L), .Label = c("2000-01-01", "2000-01-02", "2000-01-03", "2000-01-04", "2000-01-05", "2000-01-06", "2000-01-07", "2000-01-08", "2000-01-09"), class = "factor")), .Names = c("giver", "receiver", "date"), class = "data.frame", row.names = c(NA, -11L))
samp$date <- as.Date(samp$date, "%Y-%m-%d") # Format date variable
# Find first and last occurrence by date
g1 <- aggregate(samp$date, list(samp$giver), min)
colnames(g1)[1] = "company"
colnames(g1)[2] = "start.giving"
g2 <- aggregate(samp$date, list(samp$giver), max)
colnames(g2)[1] = "company"
colnames(g2)[2] = "stop.giving"
s1 <- aggregate(samp$date, list(samp$receiver), min)
colnames(s1)[1] = "company"
colnames(s1)[2] = "start.receiving"
s2 <- aggregate(samp$date, list(samp$receiver), max)
colnames(s2)[1] = "company"
colnames(s2)[2] = "stop.receiving"
# Merge data frames by company name
a1 <- merge(g1, g2, by=c("company"))
b1 <- merge(s1, s2, by=c("company"))
c1 <- merge(a1, b1, by=c("company"), all.x = TRUE)
c1 # Display desired data frame
company start.giving stop.giving start.receiving stop.receiving
A 2000-01-01 2000-01-09 2000-01-01 2000-01-09
B 2000-01-01 2000-01-01 2000-01-01 2000-01-09
C 2000-01-03 2000-01-05 2000-01-03 2000-01-06
X 2000-01-06 2000-01-06 <NA> <NA>
Y 2000-01-09 2000-01-09 <NA> <NA>
Z 2000-01-02 2000-01-08 <NA> <NA>