Skip to content

Instantly share code, notes, and snippets.

@andilabs
Last active December 21, 2015 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andilabs/6364740 to your computer and use it in GitHub Desktop.
Save andilabs/6364740 to your computer and use it in GitHub Desktop.
http://stackoverflow.com/questions/18484243/r-regex-from-string-to-two-dimensional-data-frame-in-one-command iterate over each row and split it numerical values into the columns named by the key. Example of few rows showing, how I would like it will looks like:
df <- data.frame(m = c(
"{'#JJ': 121, '#NN': 938, '#DT': 184, '#VB': 338, '#RB': 52}",
"{'#NN': 168, '#DT': 59, '#VB': 71, '#RB': 5, '#JJ': 35}",
"{'#JJ': 18, '#NN': 100, '#DT': 23, '#VB': 52, '#RB': 11}"
))
parse.one <- function(s) {
require(rjson)
y <- fromJSON(gsub("'", '"', s))
names(y) <- gsub("#", "", names(y))
as.data.frame(y)
}
library(plyr)
rbind.fill(lapply(df$m, parse.one))
# JJ NN DT VB RB
# 1 121 938 184 338 52
# 2 35 168 59 71 5
# 3 18 100 23 52 11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment