Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
887 views
in Technique[技术] by (71.8m points)

r - Remove dots from column names

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...