【问题标题】:How to move values into correct column in R?如何将值移动到 R 中的正确列中?
【发布时间】:2023-01-03 01:12:00
【问题描述】:

初学者:我有一个如下所示的数据框:

Name   Col_1     Col_2          Col_3      Col_4
abc    Website   https://LINK1  Twitter    https://LINK4
def    Email     https://LINK2  Facebook   https://LINK5
ghj    Document  https://LINK3  Website    https://LINK6

我想为 Col_1 和 Col_3(又名网站、电子邮件、文档、Twitter 和 Facebook)中的值创建列,将 Col_2 和 Col_4 中的链接移动到相应的列中。输出应如下所示:

Names  Website        Email           Document        Twitter        Facebook
abc    https://LINK1  NA              NA              https://LINK4  NA
def    NA             https://LINK2   NA              NA             https://LINK5
ghj    https://LINK6  NA              https://LINK3   NA             NA

我怎样才能做到这一点?谢谢!

【问题讨论】:

    标签: r transpose


    【解决方案1】:

    这种输出格式与所有整洁的数据约定相悖,我花了很多时间才把它弄好。

    不是最漂亮的,但它完成了工作

    library(data.table)
    mydata <- read.table(text = "Col_1     Col_2          Col_3      Col_4
    Website   https://LINK1  Twitter    https://LINK4
    Email     https://LINK2  Facebook   https://LINK5
    Document  https://LINK3  Website    https://LINK6", header = TRUE)
    
    #split in chunks of 2 columns
    L <- split.default(mydata, f = 2:(ncol(mydata) + 1) %/% 2)
    # bind rows together
    DT <- data.table::rbindlist(L, use.names = FALSE)
    # split to list by Col_1
    L2 <- split(DT, by = "Col_1", keep.by = FALSE)
    # make vectors
    L3 <- unlist(lapply(L2, c), recursive = FALSE)
    # make lengts equal
    L4 <- lapply(L3, `length<-`, max(lengths(L3)))
    # colbind
    ans <- Reduce(cbind, L4)
    # set colnames
    colnames(ans) <- names(L2)
    # make data.frame
    as.data.frame(ans)
    #         Website         Email      Document       Twitter      Facebook
    # 1 https://LINK1 https://LINK2 https://LINK3 https://LINK4 https://LINK5
    # 2 https://LINK6          <NA>          <NA>          <NA>          <NA>
    

    【讨论】:

    • 效果很好@Wimpel,谢谢!我对数据框进行了一些编辑,以包含我想保留在输出中的“名称”列,并将链接排序到正确的列中 - 对此感到抱歉,但您知道如何将其合并以获取新输出吗?
    【解决方案2】:

    这是一个选项,使用两次 pivot_widercoalesce 一旦重复的网站列如下所示:

    library(dplyr)
    library(tidyr)
    df %>%
      pivot_wider(names_from = Col_1, values_from = Col_2) %>%
      pivot_wider(names_from = Col_3, values_from = Col_4, names_repair = "unique") %>%
      mutate(Website = coalesce(`Website...2`, `Website...7`)) %>%
      select(-c(`Website...2`, `Website...7`)) 
    #> New names:
    #> • `Website` -> `Website...2`
    #> • `Website` -> `Website...7`
    #> # A tibble: 3 × 6
    #>   Name  Email         Document      Twitter       Facebook      Website      
    #>   <chr> <chr>         <chr>         <chr>         <chr>         <chr>        
    #> 1 abc   <NA>          <NA>          https://LINK4 <NA>          https://LINK1
    #> 2 def   https://LINK2 <NA>          <NA>          https://LINK5 <NA>         
    #> 3 ghj   <NA>          https://LINK3 <NA>          <NA>          https://LINK6
    

    创建于 2023-01-02 reprex v2.0.2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 2021-10-16
      相关资源
      最近更新 更多