![](/img/trans.png)
[英]How to output the sum of specific rows from one data frame to a new column in another data frame?
[英]Mutate a column based on the sum of specific rows from another data frame
我在這里尋求幫助。 我有兩個數據框,df1 和 df2。 我想根據 df2 中特定行的總和向 df1 添加一個額外的列。
Df1 包含站名。 Df2 包含以度為單位的位置、年份和觀測值。 我想要每個站的度數總和。 這些度數應該是每年特定地點的總和。 把它想象成“每個站點都應該根據給定的位置獲得每年的度數總和”。 我希望只編碼站名和位置,desired_output 中的年份應包括 df2 中給出的所有年份。
失敗的示例和所需的輸出。 我更喜歡在 tidyverse 環境中工作。
祝一切順利
df1 <- data.frame(station = c("station_A", "station_B"))
df2 <- data.frame(location= c("south", "north", "north", "east", "west"), year = c(2000, 2000, 2001, 2001, 2001), degrees = c(5,3,9,5,2))
degrees_for_each_station <-
df1%>%
mutate (degrees = case_when(
station == "station_A" ~ if_else(df2$location %in% c("north","south"),
sum(df2$degrees),
NA),
station == "station_B" ~ if_else(df2$location %in% c("north","east", "west"),
sum(df2$degrees),
NA)))
desired_output <- data.frame(station = c("station_A", "station_A","station_B", "station_B"),
year = c(2000, 2001, 2000, 2001),
degrees = c(8,9,3,16))```
一種方法是:
library(tidyverse)
df1 %>%
left_join(
df2 %>%
mutate(
location = case_when(
location == 'south' ~ 'station_A',
location %in% c('east', 'west') ~ 'station_B',
location == 'north' ~ 'station_A, station_B'
)
) %>%
separate_rows(location, sep = ', ') %>%
group_by(location, year) %>%
summarise(degrees = sum(degrees)),
by = c('station' = 'location')
)
輸出:
station year degrees
1 station_A 2000 8
2 station_A 2001 9
3 station_B 2000 3
4 station_B 2001 16
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.