I have a dataframe with several columns and I want to remove the dots on the column name. The data is as follows:
structure(list(JulDay = c(260.0208435, 260.0416565, 260.0625,
260.0833435, 260.1041565, 260.125), i.46.j.8.k.1 = c(17.99964905,
18.5903511, 22.93223381, 25.75687981, 25.78559113, 25.8169651
), i.47.j.8.k.1 = c(18.36571884, 21.15985107, 24.80698013, 25.75730324,
25.78366089, 25.65381622), i.48.j.8.k.1 = c(18.45543289, 22.02331543,
24.99463654, 25.5712738, 25.64232635, 25.66119385), i.46.j.8.k.2 = c(17.99798965,
18.60897827, 22.95389748, 25.75719261, 25.78653336, 25.81692505
), i.47.j.8.k.2 = c(18.36762619, 21.17233467, 24.82248497, 25.75767899,
25.7840023, 25.64115906), i.48.j.8.k.2 = c(18.45938683, 22.04619026,
24.9859066, 25.56440544, 25.63998032, 25.66089439), i.46.j.8.k.3 = c(17.99430084,
17.92090797, 19.67384911, 21.70389938, 23.67287827, 24.04380417
), i.47.j.8.k.3 = c(18.36631203, 19.82550049, 21.42166328, 23.76496887,
24.87460899, 25.41026688), i.48.j.8.k.3 = c(18.44684792, 20.60981369,
22.68632317, 24.1877079, 25.12503052, 24.99880028), i.46.j.8.k.4 = c(17.98368073,
17.92047691, 18.11532974, 18.67082596, 20.23210907, 21.7181263
), i.47.j.8.k.4 = c(18.33089066, 18.41581535, 18.53464127, 19.18972206,
20.14752388, 20.11002922), i.48.j.8.k.4 = c(18.21522522, 18.52231598,
19.21397209, 19.58755302, 20.11982536, 21.28104591)), .Names = c("JulDay",
"i.46.j.8.k.1", "i.47.j.8.k.1", "i.48.j.8.k.1", "i.46.j.8.k.2",
"i.47.j.8.k.2", "i.48.j.8.k.2", "i.46.j.8.k.3", "i.47.j.8.k.3",
"i.48.j.8.k.3", "i.46.j.8.k.4", "i.47.j.8.k.4", "i.48.j.8.k.4"
), row.names = c(NA, 6L), class = "data.frame")
Can anyone let me know how to remove the dots from column names ?
I want something like i46j8k1, i47j8k1, i48j8k1.
For this simple data set I could manually replace the name, but I need to do this for several data sets.
Data Visualization using R Programming First of all, create a data frame with a column having dot at last position in every value. Then, use gsub function to remove the dot at last position from every value in the column.
To remove dot and number at the end of the string, we can use gsub function. It will search for the pattern of dot and number at the end of the string in the vector then removal of the pattern can be done by using double quotes without space.
By using regexp_replace() Spark function you can replace a column's string value with another string/substring. regexp_replace() uses Java regex for matching, if the regex does not match it returns an empty string. The below example replaces the street name Rd value with Road string on address column.
library(janitor)
mydf %>%
clean_names()
The clean_names
function in janitor
package will remove any characters that are not lower-case letters, underscores, or numbers. It may convert the periods to underscores though, so if your goal is to get rid of that character completely the gsub solution will work best.
One straightforward way is to use gsub
to remove the periods from the column names:
> names(mydf)
[1] "JulDay" "i.46.j.8.k.1" "i.47.j.8.k.1" "i.48.j.8.k.1" "i.46.j.8.k.2"
[6] "i.47.j.8.k.2" "i.48.j.8.k.2" "i.46.j.8.k.3" "i.47.j.8.k.3" "i.48.j.8.k.3"
[11] "i.46.j.8.k.4" "i.47.j.8.k.4" "i.48.j.8.k.4"
> names(mydf) <- gsub("\\.", "", names(mydf))
> names(mydf)
[1] "JulDay" "i46j8k1" "i47j8k1" "i48j8k1" "i46j8k2" "i47j8k2" "i48j8k2" "i46j8k3"
[9] "i47j8k3" "i48j8k3" "i46j8k4" "i47j8k4" "i48j8k4"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With