Search code examples
rstringdtyper-colnames

Change column names' dtype from float to string?


I recently got this error while trying to put my data into tensorflow format:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
ValueError: column_name: age vocabulary dtype must be string or integer. dtype: <dtype: 'float64'>. 

Is there a command that will allow me to change the format of my data frame's column name from a float to a string?

Thank you!


Solution

  • From the NLP (Natural Language Processing) package, you can use as.String(x) to coerce the column names to a string and then store them again as the column names of the dataframe. This can be done iteratively:

    library(NLP)
    
    float_colnames <- colnames(df)
    
    string_colnames <- rep("", length(float_colnames))
    for (i in 1:length(float_colnames)) {
      string_colnames[i] <- as.String(test[i])
    }
    
    colnames(df) <- string_colnames
    

    Hope this helps!