0

enter image description here

My dataset has hundreds column of data in data.frame, every column's header name is a city's name + its state's abbreviation. I am not sure what is the "separated sign" between the city's name and state's name, I want to delete all the city's name to get a new data.frame. Like: enter image description here

I tried this way, but it doesn't work, it seems like those words are not separated by ".."

Header = c(colnames(CityValue))
split_fun = function(str){
    strsplit(str,"..")[[1]][1]
}
sapply(Header, split_fun)

I got :

enter image description here

Paul Liu
  • 31
  • 1
  • 6
  • Melt your data frame with reshape2, split *variable* into 2 columns like city & state, and sum as usually. – mlt Feb 14 '16 at 03:30
  • 1
    This is a duplicate for http://stackoverflow.com/q/7980030/673826 and let's say http://stackoverflow.com/q/7069076/673826 – mlt Feb 14 '16 at 03:39
  • How can I split variable into 2 clumns like city&state? – Paul Liu Feb 14 '16 at 20:47
  • There are many ways to skin a cat. Besides mentioned everywhere combination of strsplit, do.call, and rbind, there is off-the-shelf [colsplit from reshape2](http://www.inside-r.org/packages/cran/reshape2/docs/colsplit). – mlt Feb 15 '16 at 17:11
  • Thanks a lot, I tried use the strsplit like this: Header = c(colnames(CityValue))split_fun = function(str){ strsplit(str,",")[[1]][1] } sapply(Header, split_fun) CityValue is the name of data.frame I want split, but I found it seems like , those colnames are not separate by ".." like we can directly see, How can I know what "divided benchmark" should I use to spilt? – Paul Liu Feb 16 '16 at 03:03

0 Answers0