【问题标题】:How to pivot_wider and fill in missing values by duplicating values如何通过复制值来pivot_wider并填充缺失值
【发布时间】:2020-09-02 23:43:48
【问题描述】:

我正在尝试将数据帧从长格式转换为宽格式,但由于其结构,每当我使用 pivot_wider() 时,我都会得到两列包含数据向量的列。

这是原始数据:

structure(list(type = c("radio", "radio", "radio", "television", 
"television", "television", "television", "television", "television", 
"television", "television", "television", "television", "television", 
"television", "television", "television", "television", "television"
), Resource = c("samsung", "samsung", "samsung", "samsung", "samsung", 
"samsung", "samsung", "samsung", "samsung", "samsung", "samsung", 
"sony", "sony", "sony", "sony", "sony", "sony", "sony", "sony"
), Property = c("lot_number", "lot_number", "manufacturer", "lot_number", 
"lot_number", "lot_number", "lot_number", "lot_number", "manufacturer", 
"other_PN", "part_number", "lot_number", "lot_number", "lot_number", 
"lot_number", "lot_number", "manufacturer", "other_PN", "part_number"
), value = c("12345", "54321", "John", "9876", "12345", "54321", 
"56789", "67890", "Walt", "5g6h3f", "6789", "9876", "12345", 
"54321", "56789", "67890", "John", "2a3b4c", "3461")), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -19L), spec = structure(list(
    cols = list(type = structure(list(), class = c("collector_character", 
    "collector")), Resource = structure(list(), class = c("collector_character", 
    "collector")), Property = structure(list(), class = c("collector_character", 
    "collector")), value = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))

我使用的命令:

df_wide <- df %>% pivot_wider(names_from = Property, values_from = value)

但多个值被保存为“lot_number”列中的向量:

structure(list(type = c("radio", "television", "television"), 
    Resource = c("samsung", "samsung", "sony"), lot_number = list(
        c("12345", "54321"), c("9876", "12345", "54321", "56789", 
        "67890"), c("9876", "12345", "54321", "56789", "67890"
        )), manufacturer = list("John", "Walt", "John"), other_PN = list(
        NULL, "5g6h3f", "2a3b4c"), part_number = list(NULL, "6789", 
        "3461")), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"))

这是我最后想要得到的数据框。请注意,在所需的输出中,“radio”有一些缺失值。此外,“manufacturer”、“other_PN”和“part_number”列中的值必须在多行中重复。

structure(list(type = c("radio", "radio", "television", "television", 
"television", "television", "television", "television", "television", 
"television", "television", "television"), Resource = c("samsung", 
"samsung", "samsung", "samsung", "samsung", "samsung", "samsung", 
"sony", "sony", "sony", "sony", "sony"), manufacturer = c("John", 
"John", "Walt", "Walt", "Walt", "Walt", "Walt", "John", "John", 
"John", "John", "John"), other_PN = c(NA, NA, "5g6h3f", "5g6h3f", 
"5g6h3f", "5g6h3f", "5g6h3f", "2a3b4c", "2a3b4c", "2a3b4c", "2a3b4c", 
"2a3b4c"), part_number = c(NA, NA, 6789, 6789, 6789, 6789, 6789, 
3461, 3461, 3461, 3461, 3461), lot_number = c(12345, 54321, 9876, 
12345, 54321, 56789, 67890, 9876, 12345, 54321, 56789, 67890)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L), spec = structure(list(
    cols = list(type = structure(list(), class = c("collector_character", 
    "collector")), Resource = structure(list(), class = c("collector_character", 
    "collector")), manufacturer = structure(list(), class = c("collector_character", 
    "collector")), other_PN = structure(list(), class = c("collector_character", 
    "collector")), part_number = structure(list(), class = c("collector_double", 
    "collector")), lot_number = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))

感谢您的帮助!!

【问题讨论】:

    标签: r tidyverse


    【解决方案1】:

    我建议使用您的dput() 数据作为df 的这种方法。您可以使用group_by() 创建一个id 变量来识别行,然后您可以使用pivot_wider() 进行整形。由于需要填写一些值,您可以使用来自tidyrfill(),一个tidyverse 包:

    library(tidyverse)
    #Data
    df %>% group_by(type,Resource,Property) %>% mutate(id=1:n()) %>%
      pivot_wider(names_from = Property,values_from=value) %>%
      fill(everything()) %>% select(-id)
    

    输出:

    # A tibble: 12 x 6
    # Groups:   type, Resource [3]
       type       Resource lot_number manufacturer other_PN part_number
       <chr>      <chr>    <chr>      <chr>        <chr>    <chr>      
     1 radio      samsung  12345      John         NA       NA         
     2 radio      samsung  54321      John         NA       NA         
     3 television samsung  9876       Walt         5g6h3f   6789       
     4 television samsung  12345      Walt         5g6h3f   6789       
     5 television samsung  54321      Walt         5g6h3f   6789       
     6 television samsung  56789      Walt         5g6h3f   6789       
     7 television samsung  67890      Walt         5g6h3f   6789       
     8 television sony     9876       John         2a3b4c   3461       
     9 television sony     12345      John         2a3b4c   3461       
    10 television sony     54321      John         2a3b4c   3461       
    11 television sony     56789      John         2a3b4c   3461       
    12 television sony     67890      John         2a3b4c   3461   
    

    【讨论】:

      猜你喜欢
      • 2020-05-21
      • 2015-02-17
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多