R枢轴示例

R pivot example

我在使用 tidyr 解决这个相对论简单旋转问题时遇到了麻烦。这最好用例子来说明。我有这个未处理的数据:

data_unprocessed <- tribble(
  ~statistic, ~value,
  "median_geo_wo_nw", 2.66, 
  "median_travel_wo_nw", 4.11,
  "mean_geo_wo_nw", 12.4,
  "mean_travel_wo_nw", 34.2)

我需要像这样转换为宽格式:

data_processed <- tribble(
  ~statistic, ~geo_distance, ~travel_distance,
  "median", 2.66, 4.11,
  "mean", 12.4, 34.2)

抱歉,如果这看起来很基本,但我无法让它工作。

谢谢,

我们可以通过第一个分隔符 _ separate 'statistic' 列,然后使用 pivot_wider

library(dplyr)
library(tidyr)
library(stringr)
data_unprocessed %>% 
    separate(statistic, into = c('statistic', 'colnm'), sep="_", 
       extra = 'merge') %>% 
    mutate(colnm = str_replace(colnm, '_wo_nw', '_distance')) %>%
    pivot_wider(names_from = colnm, values_from = value)

-输出

# A tibble: 2 x 3
#  statistic geo_distance travel_distance
#  <chr>            <dbl>           <dbl>
#1 median            2.66            4.11
#2 mean             12.4            34.2 

statistic列分成两个变量:一个用于新列名,另一个用于标识新行,然后pivot_wider

data_unprocessed %>%

    mutate(
        # create a variable to name the new variables/columns
        name = if_else(grepl("geo", statistic), "geo_distance", "travel_distance"),

        # create a separate variable to name the new rows
        statistic = if_else(grepl("mean", statistic), "mean", "median")
    ) %>%

    pivot_wider(names_from = "name", values_from = "value")

结果

# A tibble: 2 x 3
  statistic geo_distance travel_distance
  <chr>            <dbl>           <dbl>
1 median            2.66            4.11
2 mean             12.4            34.2