0
$\begingroup$

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?

$\endgroup$

closed as off-topic by Nick Cox, Christoph Hanck, gung, Matt Krause, Juho Kokkala Sep 30 '15 at 12:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Nick Cox, Christoph Hanck, gung, Matt Krause, Juho Kokkala
If this question can be reworded to fit the rules in the help center, please edit the question.

0
$\begingroup$

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:

library(stringi)

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.]

$\endgroup$
  • $\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$ – Joonatan Samuel Sep 30 '15 at 11:59

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