Why am I getting X. in my column names when reading a data frame?

后端 未结 5 679
迷失自我
迷失自我 2020-11-28 06:03

I asked a question about this a few months back, and I thought the answer had solved my problem, but I ran into the problem again and the solution didn\'t work for me.

5条回答
  •  不知归路
    2020-11-28 06:16

    I ran over a similar problem and wanted to share the following lines of code to correct the column names. Certainly not perfect, since clean programming in the forehand would be better, but maybe helpful as a starting point to someone as quick and dirty approach. (I would have liked to add them as comment to Ryan's question/Gavin's answer, but my reputation is not high enough, so I had to post an additional answer - sorry).

    In my case several steps of writing and reading data produced one or more columns named "X", X.1",... containing content in the X-column and row numbers in the X.1,...-columns. In my case the content of the X-column should be used as row names and the other X.1,...-columns should be deleted.

    Correct_Colnames <- function(df) {
    
     delete.columns <- grep("(^X$)|(^X\\.)(\\d+)($)", colnames(df), perl=T)
    
      if (length(delete.columns) > 0) {
    
       row.names(df) <- as.character(df[, grep("^X$", colnames(df))])
       #other data types might apply than character or 
       #introduction of a new separate column might be suitable
    
       df <- df[,-delete.columns]
    
       colnames(df) <- gsub("^X", "",  colnames(df))
       #X might be replaced by different characters, instead of being deleted
      }
    
      return(df)
    }
    

提交回复
热议问题