简体   繁体   English

在 R 中分组时从多列数据透视表

[英]Pivot table from multiple columns while grouping in R

I am struggling to convert a wide dataset into a long one with pivot_longer in R. For the example below, I have catch data for each species as a column.我正在努力使用 R 中的 pivot_longer 将宽数据集转换为长数据集。对于下面的示例,我将每个物种的数据作为一列捕获。 I'd like to output a dataframe where the FIRST column is Species, and each row is a datapoint, with Year and Country also as columns.我想输出一个数据框,其中第一列是物种,每一行是一个数据点,年份和国家也是列。 I want to later group and average these so that I can plot them.我想稍后对这些进行分组和平均,以便我可以绘制它们。 Does anyone know how to achieve this with pivot_longer?有谁知道如何使用 pivot_longer 实现这一目标?

 Data <- data.frame(
 Country = c("a", "a", "c", "c", "a", "b"),
 Year = c("1990", "1990", "1991", "1992", "1990", "1990"),
 Tiger_Shark = c(0,1,4,7,5,6)  ,
 Whale_shark = c(0, 20, 14, 19, 2,7),
 White_shark = c(0, 0, 12, 29, 1,8))

I have tried the following:我尝试了以下方法:

data %>% 
pivot_longer( cols= Tiger_Shark:White_shark,  
          names_to = c(" Tiger", "Whale", "White"),
          values_to = "catch") 

But this does not retain the Year or Country columns, which I'd like to keep.但这不会保留我想保留的 Year 或 Country 列。 I think the solution is simple but I am not familiar with pivot_longer.我认为解决方案很简单,但我对pivot_longer 不熟悉。 Thank you so much!!非常感谢!!

If we want to use ggplot , reshape to 'long' format and then plot.如果我们想使用ggplot ,请重塑为“长”格式,然后进行绘图。 The summarisation can be done within summarise after grouping and then do the reshaping可以在分组后的summary内进行summarise ,然后进行整形

library(dplyr)
library(tidyr)
library(ggplot2)
Data %>%
   group_by(Year, Country) %>% 
   summarise(across(everything(), mean)) %>%
   pivot_longer(cols = Tiger_Shark:White_shark) %>%
   ggplot(aes(x = Country, y = value, fill = Year)) + 
     geom_col()

Or first reshape to 'long' format and then do a group by summarise或者先重塑为“长”格式,然后通过汇总进行分组

Data %>% 
 pivot_longer(cols = Tiger_Shark:White_shark) %>%
 group_by(Country, Year) %>% 
 summarise(value = mean(value))

The names_to should be a single element here as these columns seems to belong to a single group names_to在这里应该是单个元素,因为这些列似乎属于一个组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM