I have a dataframe I pulled into R using sqlQuery . I'd like to strip all the whitespace and special characters out of the data.frame's names, but sqlQuery has no strip.white=TRUE
option, so I was thinking of doing this with a regular expression.
This works for whitespace:
myNames <- c("Sample Selection Reason", "My ID")
myNames <- gsub('\\s+', '.', myNames )
What can I do about special characters?
You might like make.names()
, a base R function that "make[s] syntactically valid names out of character vectors."
myNames <- c("Sample Selection Reason", "My ID")
make.names(myNames)
# [1] "Sample.Selection.Reason" "My.ID"
data.frame(...) corrects the names. For example
df.badnames <- data.frame(`1-2` = 1:2, check.names=FALSE)
df.fixed <- data.frame(df)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.