I have a CSV file that has a value with comma inside it : "Afganis,tan"
I would like to replace the comma with "_" so I use the following gsub
statments.
However when I print the outcome dataframe I see that the value is broken into two parts: one is in the same column ("Afganis") and the second ("tan") was inserted to next column. How can I overcome this issue so I'll get "Afganis_tan"? Is there a way to insert the gsub
function into the read.csv
function ?
Attached is the related code that I've tried.
df<- read.csv(inFile$datapath, header = input$header, sep = input$sep, quote = "")
ind<-sapply(1:ncol(df), function(x) { class(df[,x])=="factor"})
print(ind)
ind<-which(ind==TRUE)
if(length(ind)==1){
df[,ind]<-gsub('[^a-zA-Z0-9.]',"_",as.character(df[,ind]))
df[,ind]<-df('\\,',"_",as.character(df[,ind]))
df[,ind]<-df(',',"_",as.character(df[,ind]))
df[,ind]<-as.factor(df[,ind])
}
Here is the output (line 9 is the problematic):
campaign_type country_name transaction_type cpa_price TIERS Year_Success_20.
1 CPM Afgha nistan Domestic 0 TIER_4_Asia 0
2 CPM Afg' hanistan Domestic 0 TIER_4_Asia 0
3 CPM Afgh' (anistan) Cross Optimized Advertiser 0 TIER_4_Asia 1
4 CPM Afgha-nistan Domestic 0 TIER_4_Asia 0
5 CPM "Afghan""istan""" Cross Optimized Advertiser 0 TIER_4_Asia 0
6 CPM Afghan~!#$%^@&*()_+|istan Domestic 0 TIER_4_Asia 0
7 CPM Afghanistan Domestic 0 TIER_4_Asia 0
8 CPM Afghanistan. Domestic 0 TIER_4_Asia 0
9 CPM "Afghan istan" Domestic 0.040995321 TIER_4_Asia
10
11 CPM Afghanistan Domestic 0 TIER_4_Asia 1
Attached is link to the file. File with versions to the string "Afganistan"