【问题标题】:R pivot exampleR 枢轴示例
【发布时间】:2021-04-13 18:46:07
【问题描述】:

我在 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)

对不起,如果这看起来很简单,但我无法让它工作。

谢谢,

【问题讨论】:

  • tidyr::pivot_wider()

标签: r tidyverse tidyr


【解决方案1】:

我们可以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 

【讨论】:

    【解决方案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 
    

    【讨论】:

      猜你喜欢
      • 2013-12-28
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      • 2014-02-25
      • 1970-01-01
      相关资源
      最近更新 更多