I often work with tables full with special characters (fe á,ľ,š,č,ť,ž,ý,á,í,é,... etc.). I found very useful function called mgsub
which can do simultaneous multiple string replacement. I works well of vector, however I am struggling to apply which function to whole dataframe.
Function mgsub
work like this:
library(mgsub)
mgsub::mgsub("...A čo i tam dušu dáš v tom boji divokom: Mor ty len, a voľ nebyť, ako byť otrokom.",
pattern = c(".","A","č","š","á",":",",","ľ","ť","M"," "),
replacement = c("","a","c","s","a","","","","t","m",""), fixed = TRUE)
[1] "acoitamdusudasvtombojidivokommortylenavonebytakobytotrokom"
But how to apply this function to whole data.frame? For example on this data.frame...
my.df <- data.frame(v1 = c("...A čo i tam dušu","dáš v tom boji"),
v2 = c("divokom:","Mor ty len,"),
v3 = c("a voľ nebyť,","ako byť otrokom."))
v1 v2 v3
1 ...A čo i tam dušu divokom: a voľ nebyť,
2 dáš v tom boji Mor ty len, ako byť otrokom.
I tried to aplly lapply. but it gives only errors...
data.frame(lapply(my.df, mgsub::mgsub,
pattern = c(".","A","č","š","á",":",",","ľ","ť","M"," "),
replacement = c("","a","c","s","a","","","","t","m",""), fixed = TRUE))
Error in nchar(string) : 'nchar()' requires a character vector
Any suggestions are welcome.
The issue is that the columns are factor
and mgsub
requires a character
input. According to ?mgsub
string - a character vector where replacements are sought
Either convert all the columns to character
class
my.df[] <- lapply(my.df, as.character)
Or use type.convert
my.df <- type.convert(my.df, as.is = TRUE)
Or use stringsAsFactors = FALSE
while creating the data.frame
as the default option in data.frame
is stringsAsFactors = TRUE
my.df <- data.frame(v1 = c("...A čo i tam dušu","dáš v tom boji"),
v2 = c("divokom:","Mor ty len,"),
v3 = c("a voľ nebyť,","ako byť otrokom."),
stringsAsFactors = FALSE)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.