簡體   English   中英

Google表格上的R腳本

[英]R Scripting on google sheets

我有一個看起來像這樣的谷歌表

Name  Surname  Country  Path
John   Snow      UK     /Home/drive/John 
BOB    Anderson  USA    /Home/drive/BOB
Tim    David     UK     /Home/drive/Tim 
Wayne  Green     UK     /Home/drive/Wayne

我編寫了一個腳本,該腳本首先檢查country =="UK"是否為true,如果使用R中的gsub將Path從"/Home/drive/"更改為"/Server/files/"

腳本

Pattern<-"/Home/drive/"

Replacement<- "/Server/files/"

gs_catalog_Staging <- apply(gs_catalog$Sheet_Name[4],function(x){
ifelse(gs_catalog$Sheet_Name[3]== "UK", gsub(Pattern,Replacement, gs_catalog$Sheet_Name[4],ignore.case=T),gs_catalog) })

我得到的輸出(我只放第一行以節省空間):

Name  Surname  Country  Path
John   Snow      UK     c("/Server/files/John"), c("/Server/files/BOB"), c("/Server/files/BOB"),c("/Server/files/Tim"), c("/Server/files/Wayne")

我想要的輸出

Name  Surname  Country  Path
John   Snow      UK     /Server/files/John

謝謝

嘗試這個:

Pattern<-"/Home/drive/"

Replacement<- "/Server/files/"

gs_catalog <- read.table(text = "Name  Surname  Country  Path
John   Snow      UK     /Home/drive/John 
BOB    Anderson  USA    /Home/drive/BOB
Tim    David     UK     /Home/drive/Tim 
Wayne  Green     UK     /Home/drive/Wayne", 
                         header = TRUE, as.is = TRUE
)

gs_catalog$Path <- ifelse(gs_catalog$Country == 'UK',
                          sub(Pattern, Replacement, gs_catalog$Path),
                          gs_catalog$Path
)

結果:

> gs_catalog
   Name  Surname Country                Path
1  John     Snow      UK  /Server/files/John
2   BOB Anderson     USA     /Home/drive/BOB
3   Tim    David      UK   /Server/files/Tim
4 Wayne    Green      UK /Server/files/Wayne

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM