I have multi-class classification dataset with data saved in the form.

[number, ..., number],number,number... ,number \n

The numbers inside brackets are the labels and have varying length. Numbers after are features with fixed length $n$. How to parse this into a R table?


1 Answer 1


There probably is some perfect way to load everything in one fell swoop but since it sounds like you need a quick-and-dirty solution, you could try something along the lines of:


df <- read.table(my.table, header=FALSE, sep=']', stringsAsFactors = FALSE)
vals <- stri_split_coll(df$V2, pattern=',', simplify=TRUE, omit_empty = TRUE)
vals <- data.frame(apply(vals, 2, as.numeric))

labels <- stri_replace(df$V1, '', fixed = '[')
rownames(vals) <- labels

Does this get the job done for you?

[Note: If your data has a header then just add the argument "skip=1" to read.table and either add the columns manually or run the function again (with "nrows=1"), etc.]

  • $\begingroup$ Yep, quick and dirty is fine. Getting vals and labels out fine all that is needed. Can't assign rownames(vals) <- labels, since there is requirement for rownames to be unique. Got my data tho, accepted. Thank you! $\endgroup$ Commented Sep 30, 2015 at 11:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.