[英]Creating row-wise pairs in R
我正在嘗試將行配對以用於啞鈴 plot。我有一個看起來像這樣的 df:
年 | 物種 | 噸 |
---|---|---|
1960 | 鱈魚 | 123 |
1961年 | 鱈魚 | 456 |
1970 | 鱈魚 | 124 |
1971年 | 鱈魚 | 457 |
我想將相隔 10 年的向上結果配對,從而得到這個 df:
年 | 物種 | 噸 | 一對 |
---|---|---|---|
1960 | 鱈魚 | 123 | 1個 |
1961年 | 鱈魚 | 456 | 2個 |
1970 | 鱈魚 | 124 | 1個 |
1971年 | 鱈魚 | 457 | 2個 |
我非常感謝幫助。 我不太確定從哪里開始解決這個問題。
你可以做
df <- structure(list(Year = c(1960L, 1961L, 1970L, 1971L), Species = c("Cod",
"Cod", "Cod", "Cod"), Tonnes = c(123, 150, 256, 450)), row.names = c(NA,
-4L), class = "data.frame")
library(tidyverse)
df %>%
mutate(year = Year %% 10,
decade = 10 * Year %/% 10) %>%
select(-Year) %>%
group_by(Species, year) %>%
summarize(from = Tonnes[which.min(decade)],
to = Tonnes[which.max(decade)],
year = paste(min(year + decade), max(year + decade), sep = '-')) %>%
ggplot(aes(from, year)) +
geom_linerange(aes(xmin = from, xmax = to), alpha = 0.5) +
geom_point(color = 'green4', size = 3) +
geom_point(aes(x = to), color = 'red3', size = 3) +
xlab('Tonnes') +
theme_minimal(base_size = 16)
使用data.table
,連接將獲得寬格式的對:
library(data.table)
dt <- setDT(df)[
, `:=`(Year2 = Year + 10, Pair = rleid(Year, Species))
][
df,
.(Year1 = i.Year, Year2 = x.Year, Species, Tonnes1 = i.Tonnes, Tonnes2 = Tonnes, Pair = i.Pair),
on = .(Year = Year2, Species), nomatch = 0
]
dt
#> Year1 Year2 Species Tonnes1 Tonnes2 Pair
#> 1: 1960 1970 Cod 123 124 1
#> 2: 1961 1971 Cod 456 457 2
如果需要,可以將其融化為長格式:
setcolorder(
melt(dt, c("Species", "Pair"), list(c("Year1", "Year2"), c("Tonnes1", "Tonnes2")), value.name = c("Year", "Tonnes")),
c("Year", "Species", "Tonnes", "Pair")
)[, variable := NULL][]
#> Year Species Tonnes Pair
#> 1: 1960 Cod 123 1
#> 2: 1961 Cod 456 2
#> 3: 1970 Cod 124 1
#> 4: 1971 Cod 457 2
數據:
df <- data.frame(Year = c(1960, 1961, 1970, 1971), Species = "Cod", Tonnes = c(123, 456, 124, 457))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.