oercim
oercim

Reputation: 1848

Removing specific elements from a factor column of dataframe

Let I have such a dataframe where the column elements are factors:

head1
------
jfd.
kl.df
hgg
err.r

I want to remove dots from each level. Namely, the output should be like:

head2
------
jfd
kldf
hgg
errr

I tried sub and gsub functions but however they didn't work. I think they didin't work because being factors. I tried to convert the factors into character but I couldn't manage it too.

How can I remove dots from the related columns? I will be very glad for any help. Thanks a lot.

Upvotes: 0

Views: 1758

Answers (1)

user6022341
user6022341

Reputation:

You can try something like this:

levels(df$head1) <- gsub(".", "", levels(df$head1), fixed=TRUE)

Or:

df$head1 <- gsub(".", "", as.character(df$head1), fixed=TRUE)

Or:

df$head1 <- sub(".", "", df$head1, fixed=TRUE)

Upvotes: 1

Related Questions