19

我有一个包含几列的数据框,我想删除列名上的点。数据如下:

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")

谁能让我知道如何从列名中删除点?

我想要像 i46j8k1、i47j8k1、i48j8k1 这样的东西。

对于这个简单的数据集,我可以手动替换名称,但我需要对多个数据集执行此操作。

4

2 回答 2

36

一种直接的方法是使用gsub从列名中删除句点:

> 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"
于 2013-04-16T16:12:18.837 回答
8
library(janitor)

mydf %>%
  clean_names()

包中的clean_names函数janitor将删除任何不是小写字母、下划线或数字的字符。不过,它可能会将句点转换为下划线,因此如果您的目标是完全摆脱该字符,则 gsub 解决方案将最有效。

于 2017-06-07T22:52:24.053 回答