【发布时间】: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"
【问题讨论】: