Last active
December 21, 2015 20:58
-
-
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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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