r - Splitting numerals from string in data frame -


this question has answer here:

i have data frame in r column looks this:

venue aaa 2001 bbb 2016 ccc 1996 ... .... zzz 2007 

in order make working dataframe easier wanted split venue column 2 columns, location , year, so:

location year aaa      2001 bbb      2016 ccc      1996 ...      .... zzz      2007 

i have tried various variations of csplit() function achieve this:

df = csplit(df, "venue", " ") #worked somewhat, issues places multiple words (e.g. los angeles, rio de janeiro) df = csplit(df, "venue", "[:digit:]") df = csplit(df, "venue,", "[0-9]+") 

none of these worked far me. i'd appreciate if point me in right direction.

how this?

d <- data.frame(venue = c("aaa 2001", "bbb 2016", "ccc 1996", "cc d 2001"),          stringsasfactors = false)  d$location <- gsub("[[:digit:]]", "", d$venue) d$year <- gsub("[^[:digit:]]", "", d$venue) d #       venue location year # 1  aaa 2001     aaa  2001 # 2  bbb 2016     bbb  2016 # 3  ccc 1996     ccc  1996 # 4 cc d 2001    cc d  2001 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -