【问题标题】:Keep the last n columns only outputted by separate by delimiter保持最后 n 列仅由分隔符分隔输出
【发布时间】:2020-01-03 09:15:49
【问题描述】:

我有一个带有以下因子变量的数据框:

> head(example.df)
                                                                                      path
1 C:/Users/My PC/pinkhipppos/tinyhorsefeet/location1/categoryA/eyoshdzjow_random_image.txt

(组成目录)。

我想根据分隔符拆分为单独的列:/

我可以使用

library(tidyverse)

example.df <- example.df %>% 
  separate(path,
           into=c("dir",
                  "ok",
                  "hello",
                  "etc...",
                  "finally...",
                  "location",
                  "category",
                  "filename"),
           sep="/")

虽然,我只对最后两个目录和文件名或来自单独函数的最后 3 个结果感兴趣。由于父目录(高于位置)可能会改变。我想要的输出是:

> head(example.df)
       location       category                       filename
1     location1      categoryA    eyoshdzjow_random_image.txt

可重现性:

example.df <- as.data.frame(
  c("C:/Users/My PC/pinkhipppos/tinyhorsefeet/location1/categoryA/eyoshdzjow_random_image.txt",
    "C:/Users/My PC/pinkhipppos/tinyhorsefeet/location2/categoryB/jdugnbtudg_random_image.txt")
)

colnames(example.df)<-"path"

【问题讨论】:

    标签: r tidyverse delimiter


    【解决方案1】:

    base R 中的一种方法是在"/" 处拆分字符串并从每个列表中选择最后三个元素。

    as.data.frame(t(sapply(strsplit(as.character(example.df$path), "/"), tail, 3)))
    
    #         V1        V2                          V3
    #1 location1 categoryA eyoshdzjow_random_image.txt
    #2 location2 categoryB jdugnbtudg_random_image.txt
    

    使用tidyverse,我们可以获取长格式数据,选择每行最后3个条目,获取宽格式数据。

    library(tidyverse)
    
    example.df %>%
      mutate(row = row_number()) %>%
      separate_rows(path, sep = "/") %>%
      group_by(row) %>%
      slice((n() - 2) : n()) %>%
      mutate(cols = c('location', 'category', 'filename')) %>%
      pivot_wider(names_from = cols, values_from = path) %>%
      ungroup() %>%
      select(-row)
    
    # A tibble: 2 x 3
    #  location  category  filename                   
    #  <chr>     <chr>     <chr>                      
    #1 location1 categoryA eyoshdzjow_random_image.txt
    #2 location2 categoryB jdugnbtudg_random_image.txt
    

    或类似于基本 R 的概念,但使用 tidyverse

    example.df %>%
      mutate(temp = map(str_split(path, "/"), tail, 3)) %>%
      unnest_wider(temp, names_repair = ~paste0("dir", seq_along(.) - 1)) %>%
      select(-dir0)
    

    【讨论】:

    • 优秀。我会接受的。也会对 tidyverse 解决方案感兴趣。非常感谢。
    • 添加了tidyverse 解决方案。
    猜你喜欢
    • 2013-07-14
    • 2012-12-21
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多