I have two data frames with a column named โHeaderโ in each containing a row. I need to reduce these lines in order to combine them. Now I want to make it as clean as possible in a loop, so I only need to write a gsub function only once.
Say I have:
df_1 <-read.table(text=" id Title 1 some_average_title 2 another:_one 3 the_third! 4 and_'the'_last ",header=TRUE,sep="")
and
df_2 <-read.table(text=" id Title 1 some_average.title 2 another:one 3 the_third 4 and_the_last ",header=TRUE,sep="")
Now I run:
df_1$Title <- gsub(" |\\.|'|:|!|\\'|_", "", df_1$Title ) df_2$Title <- gsub(" |\\.|'|:|!|\\'|_", "", df_2$Title )
I tried the following loop:
for (dtfrm in c("dt_1", "df_2")) { assign(paste0(dtfrm, "$Title"), gsub(" |\\.|'|:|!|\\'|", "", get(paste0(dtfrm, "$Title"))) ) }
but it does not work - despite the absence of error messages.
I also thought about lapply(list(dt_1, dt_2), function(w){ w$Title <- XXX }) , but I do not know what to put for XXX because gsub() needs the third argument in the list of strings.
source share