r - Automatic split of character matrix according to a column values into variable number of new dataframes -


i split character matrix have according 1 of column values. if example have 3 columns , "n" rows, , want use column number 2 reference. script should in second column , group rows contain same value dataframe.

so, have "a", "b", "c", "d" , "e" values in column 2 through "n" rows. want (in case) 5 new dataframes containing rows of data conditioned second column values. rows contain "a" in second column of matrix go 1 dataframe , on.

my data bigger, containing around 400 different character values in column want use reference (column 2 in above example) split process needs automatic, mean, has automatically detect how many new dataframes should created according number of different values in "column 2".

here shorter example of need:

structure(c("hi", "med", "hi", "low", "a", "d", "a", "c", "8",  "3", "9", "9", "1", "1", "1", "2"), .dim = c(4l, 4l), .dimnames = list(     null, c("b", "x", "y", "z"))) 

here need have 3 new dataframes if use (again) column 2 ("x") reference. 1 dataframe containing rows 1 , 3, dataframe containing row 2 , final 1 containing row 4, there 3 different values in column: "a", "d" , "c".

the new dataframes should named automatically value being grouped with. first dataframe should named "a", second "d" , on. possible make process automatic bigger data?

i hope clear enough, , sorry if answered before couldnt find solution worked me.

by sounds of it, you're looking split function.

x <- structure(c("hi", "med", "hi", "low",                   "a", "d", "a", "c",                   "8", "3", "9", "9",                   "1", "1", "1", "2"),                 .dim = c(4l, 4l),                 .dimnames = list(null, c("b", "x", "y", "z"))) split(data.frame(x), x[, 2]) # $a #    b x y z # 1 hi 8 1 # 3 hi 9 1 #  # $c #     b x y z # 4 low c 9 2 #  # $d #     b x y z # 2 med d 3 1 

the resulting data.frames in single list, can things assign if want split them individual data.frames in workspace.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -