Using syntactically difficult strings as column names in a data frame

冷暖自知 提交于 2019-12-02 07:59:11

You don’t need to use make.names at all — you can assign the strings directly. This works perfectly fine in R. You just need to backtick-quote the names when you try to use them as R names (e.g. after the $ operator):

names(df) = unlist(df[1,])
df$`Some messy string to be used`

use stringsAsFactors = F in data.frame which will create columns as char instead of factors. then make names on it.

df <- data.frame(A=c("Some messy string to be used",222,0), 
             B=c("Very important ? indicator from 2001", 888, 44),
             C=c("001 This variable / makes no sense", 888, 44),
             D=c("Geography", 1, 2),stringsAsFactors = F)
names(df) <- make.names(df[1,])
names(df)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!