Using syntactically difficult strings as column names in a data frame

后端 未结 2 1256
天涯浪人
天涯浪人 2021-01-26 03:05

I\'m working with a data frame similar to the extract below:

df <- data.frame(A=c(\"Some messy string to be used\",222,0), 
                 B=c(\"Very import         


        
2条回答
  •  自闭症患者
    2021-01-26 03:53

    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)
    

提交回复
热议问题