【问题标题】:Splitting string in r and putting them in new columns in new data frame在 r 中拆分字符串并将它们放入新数据框中的新列中
【发布时间】:2014-10-12 09:35:51
【问题描述】:

我想在 R 中执行此操作。 我运行代码来提取所有文件夹和子文件夹的路径。我得到下面提到的列表。我想对此应用一组规则:

  1. 如果在整行中遇到 1 "/",则将“/”替换为 "/Folder/"
  2. 如果在整行中遇到 2 "/",则什么也不做。
  3. 如果遇到 3 个或更多 "/",则忽略第一个和最后一个“/”并将所有剩余的“/”替换为 "-"

我运行的代码是提取文件路径是:

  b<-list.files(path="/Users/Mohit/Desktop/Company/Database",recursive=TRUE)

  [1] "Accounts/Academic History.pdf"                       "Accounts/Contract.pdf"                              
  [3] "Accounts/Credit/Analyst/Banking/TFileOutput.txt"     "Accounts/Credit/Analyst/untitled.jpg"               
  [5] "Accounts/Credit/background.jpg"                      "Accounts/Credit/background.xcf"                     
  [7] "Accounts/Debit/index.html"                           "Human Resources/RStudio-0.98.1073.dmg"              
  [9] "Information Technology/Iti.pdf"                      "Logistics/1610085_10152585224658626_398303669_n.jpg"
  [11] "Sales/947309_10152376144413626_1056138683_n.jpg"    

我无法理解要使用哪个功能。带 sapply 的 stringr 包可能吗? 我想把它放在一个带有标题的列中并将其导出为文本文件。

任何帮助将不胜感激。

非常感谢

【问题讨论】:

  • 我为我的代码出现的方式道歉。它应该好多了。有11行
  • 我更新了新信息。检查是否有帮助。
  • 非常感谢。它对我帮助很大。我试图使用你没有使用的 sapply。你没有使用 sapply 有什么原因吗?
  • sapply 和apply 系列函数在某些方面隐含使用loops。这里ifelse是矢量化的,所以我用它代替sapply

标签: r split character


【解决方案1】:

这可能会有所帮助:

library(stringr)
Ct <- str_count(b, "/")
b1 <- ifelse(Ct==1, gsub("[/]", "/Folder/", b), 
        ifelse(Ct>=3, gsub("(^([^/]+[/])|([/][^/]+)$)(*SKIP)(*F)|[/]", "-",b,
                                         perl=TRUE), b))

 b1
 #[1] "Accounts/Folder/Academic History.pdf"                      
 #[2] "Accounts/Folder/Contract.pdf"                              
 #[3] "Accounts/Credit-Analyst-Banking/TFileOutput.txt"           
 #[4] "Accounts/Credit-Analyst/untitled.jpg"                      
 #[5] "Accounts/Credit/background.jpg"                            
 #[6] "Accounts/Credit/background.xcf"                            
 #[7] "Accounts/Debit/index.html"                                 
 #[8] "Human Resources/Folder/RStudio-0.98.1073.dmg"              
 #[9] "Information Technology/Folder/Iti.pdf"                     
 #[10] "Logistics/Folder/1610085_10152585224658626_398303669_n.jpg"
 #[11] "Sales/Folder/947309_10152376144413626_1056138683_n.jpg"    

如果要创建data.frame,然后导出为.txt文件

   dat <- data.frame(b, b1, stringsAsFactors=FALSE)
   write.table(dat, file="Mohit.txt", quote=FALSE, row.names=FALSE, sep=",")

更新

如果需要根据b1创建3列

  datN <- setNames(read.table(text=b1, sep="/"), c("Class", "Title", "File"))
  head(datN,2)
  #     Class  Title                 File
  #1 Accounts Folder Academic History.pdf
  #2 Accounts Folder         Contract.pdf

现在,您可以使用write.table 保存文件

数据

 b <- c("Accounts/Academic History.pdf", "Accounts/Contract.pdf", "Accounts/Credit/Analyst/Banking/TFileOutput.txt", 
 "Accounts/Credit/Analyst/untitled.jpg", "Accounts/Credit/background.jpg", 
 "Accounts/Credit/background.xcf", "Accounts/Debit/index.html", 
 "Human Resources/RStudio-0.98.1073.dmg", "Information Technology/Iti.pdf", 
 "Logistics/1610085_10152585224658626_398303669_n.jpg","Sales/947309_10152376144413626_1056138683_n.jpg"

【讨论】:

  • 您可能想要添加显示如何将 b1 添加为列然后导出到文本文件。
  • @user2923027 没问题。祝你周末愉快!
  • 我现在想根据“/”将其拆分为 3 列 - 类、标题和文件,然后将其导出到文本文件中。我将使用 str_split(b1,"/")。对吗?
  • 您可能想开始一个新问题,提供到目前为止的代码,并阐明您想要输出的内容。
  • 我的错。真的对不起。在我的第一个问题中,我想将“/”替换为“/Folder/”,尽管我更正了我的问题,但我忘了向您提及最后一个斜线。现在所有的行都将有 3 个“/”
猜你喜欢
  • 2021-05-31
  • 2016-11-21
  • 1970-01-01
  • 2022-09-30
  • 2016-11-07
  • 2018-02-17
  • 1970-01-01
  • 1970-01-01
  • 2021-01-08
相关资源
最近更新 更多