简体   繁体   中英

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.

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)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM