How to replace the “.” in column names generated by read.csv() with a single space when exporting?

前端 未结 4 1797
悲&欢浪女
悲&欢浪女 2020-12-24 13:25

I am using R to do some data pre-processing, and here is the problem that I am faced with: I input the data using read.csv(filename,header=TRUE), and then the s

4条回答
  •  伪装坚强ぢ
    2020-12-24 14:15

    Just to add to the answers already provided, here is another way of replacing the “.” or any other kind of punctation in column names by using a regex with the stringr package in the way like:

    require(“stringr”)   
    colnames(data) <- str_replace_all(colnames(data), "[:punct:]", " ")
    

    For example try:

    data <- data.frame(variable.x = 1:10, variable.y = 21:30, variable.z = "const")
    
    colnames(data) <- str_replace_all(colnames(data), "[:punct:]", " ")
    

    and

    colnames(data)
    

    will give you

    [1] "variable x" "variable y" "variable z"
    

提交回复
热议问题