将列转换为行并在 R 中重新排列 table

Convert columns into rows and rearrange table in R

我目前有一个 table 看起来像这样:

table <- data.frame("Season" = c("Winter", "Winter", "Winter", "Winter", "Winter", "Winter",
                             "Summer", "Summer", "Summer", "Summer", "Summer", "Summer"),
                "Site" = c(123,123,123,123,123,123,123,123,123,123,123,123),
                "Flow" = c("H001", "H007", "H030", "L001", "L007", "L030",
                           "H001", "H007", "H030", "L001", "L007", "L030"),
                "X0.5" = c(100,200,300,400,500,600,700,800,900,1000,1100,1200),
                "X0.2" =c(1,2,3,4,5,6,7,8,9,10,11,12),
                "X0.1" = c(12,11,10,9,8,7,6,5,4,3,2,1),
                "X0.05" = c(10, 11, 12, 1,2,3,4,5,6,7,8,9),
                "X0.04" = c(2,4,6,8,10,12,14,16,10, 10, 10, 10))

这就是我最后想要的 table(但具有正确的对应值,而不是全 0)

table2 <- data.frame("Site" = c(123, 123, 123, 123, 123), "Recurrence_Interval" = c(0.5, 0.2, 0.1, 0.05, 0.04), 
                 "H001_Winter" = c(0,0,0,0,0), "H007_Winter" = c(0,0,0,0,0), "H030_Winter" = c(0,0,0,0,0),
                 "L001_Winter" = c(0,0,0,0,0), "L007_Winter" = c(0,0,0,0,0), "L030_Winter" = c(0,0,0,0,0),
                 "H001_Summer" = c(0,0,0,0,0), "H007_Summer" = c(0,0,0,0,0), "H030_Summer" = c(0,0,0,0,0),
                 "L001_Summer" = c(0,0,0,0,0), "L007_Summer" = c(0,0,0,0,0), "L030_Summer" = c(0,0,0,0,0))

谢谢!!

已编辑以显示更真实的内容table

我们可以 unite 两列,然后使用 pivot_wider

重塑为 'wide' 格式
library(dplyr)
library(tidyr)
library(data.table)
table %>% 
   unite(Season_Flow, Flow, Season, sep="_") %>% 
   pivot_longer(cols = starts_with('X0'), names_to = 'Recurrence_Interval') %>%
   mutate(rn = rowid(Season_Flow, Site), 
     Recurrence_Interval = readr::parse_number(Recurrence_Interval)) %>%
   pivot_wider(names_from = Season_Flow, values_from = value)

更新

根据OP提供的不同数据,我们可以做

library(readxl)
df1 <- read_excel(file.choose())
df1 %>% 
   unite(Season_Flow, Flow, Season, sep="_") %>%
   pivot_longer(cols = matches("^[0-9]"),  names_to = 'Recurrence_Interval') %>%
   type.convert %>% 
   select(-Method, -Name, -n, -Regulated, -`KS Test`) %>%
   mutate(rn = rowid(Season_Flow, Site_No)) %>%
   pivot_wider(names_from = Season_Flow, values_from = value) %>% 
   as.data.frame
#  Site_No Recurrence_Interval rn H001_Annual H007_Annual H030_Annual L007_Annual L030_Annual L001_Annual L007_Winter H001_Winter
#1     123                0.50  1       525.5     202.215    108.9350    15.68600    21.32300     13.3000     23.1290       441.0
#2     123                0.20  2       782.2     320.972    159.0300    11.24960    15.67940      9.1040     16.6856       623.2
#3     123                0.10  3      1014.0     390.086    195.7190     9.27142    11.85660      7.8200     12.4340       699.2
#4     123                0.05  4      1108.5     458.186    214.7105     7.90284     9.43433      6.1300     11.2001      1072.0
#5     123                0.04  5      1394.1     489.760    218.5800     7.87730    10.26500      2.9589     10.2370      1050.2
#  H007_Winter H030_Winter L030_Winter L001_Winter
#1     171.270     102.120     29.9000     20.0000
#2     247.772     135.232     22.9466     13.4000
#3     315.088     159.790     18.3064     11.0000
#4     379.704     193.658     13.7830      8.2880
#5     381.810     192.770     14.1070      4.6929