[英]Splitting string in r and putting them in new columns in new data frame
我想在R中執行此操作。我運行代碼以提取所有文件夾和子文件夾的路徑。 我得到下面提到的清單。 我想對此應用一套規則:
"/"
則用"/Folder/"
替換該“ "/Folder/"
"/"
,則不執行任何操作。 "/"
,則忽略第一個和最后一個“ /”,並將所有剩余的“ /”替換為"-"
我運行的代碼是提取文件路徑為:
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的stringer包? 我想將其放在帶有標題的列中,並將其導出為文本文件。
任何幫助將不勝感激。
非常感謝
可能會有所幫助:
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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.