How to delete '.' from column names in a data frame?

My data frame, which I read from the csv file, has column names like

abc.def, ewf.asd.fkl, qqit.vsf.addw.coil

I want to remove the '.' from all names and convert them to

abcdef, eqfasdfkl, qqitvsfaddwcoil.

I tried using the sub subcommand sub(".","",colnames(dataframe)) , but this command pulled out the first letter of each column name, and the column names were changed to

bc.def, wf.asd.fkl, qit.vsf.addw.coil

Does anyone know another team for this. I can change the column name one by one, but I have many files with 30 or more columns in each file.

Again, I want to remove the "." of all the names. I am trying to do this, so I can use the "sqldf" commands that are not related to "."

thanks for the help

+6
source share
2 answers

1) sqldf can handle names that have periods in them if you specify names:

 library(sqldf) d0 <- read.csv(text = "AB,CD\n1,2") sqldf('select "AB", "CD" from d0') 

giving:

  AB CD 1 1 2 

2) When reading data using read.table or read.csv use the argument check.names=FALSE .

For comparison:

 Lines <- "AB,CD 1,2 3,4" read.csv(text = Lines) ## AB CD ## 1 1 2 ## 2 3 4 read.csv(text = Lines, check.names = FALSE) ## ABCD ## 1 1 2 ## 2 3 4 

however, in this example, it still leaves the name that should be specified in sqldf, since the names have built-in spaces.

3) To simply delete periods if DF is a data frame:

 names(DF) <- gsub(".", "", names(DF), fixed = TRUE) 

or it would be better to convert periods to underscores so that they are reversible:

 names(DF) <- gsub(".", "_", names(DF), fixed = TRUE) 

This last line can be done as follows:

 names(DF) <- chartr(".", "_", names(DF)) 
+11
source

To replace all the dots in the names, you will need to use gsub, not sub, which will replace only the first occurrence.

That should work.

 test <- data.frame(abc.def = NA, ewf.asd.fkl = NA, qqit.vsf.addw.coil = NA) names(test) <- gsub( ".", "", names(test), fixed = TRUE) test abcdef ewfasdfkl qqitvsfaddwcoil 1 NA NA NA 
+5
source

Source: https://habr.com/ru/post/986005/


All Articles