-1

If I have a data table in R like this:

Table0:

some subtitle 1
info1a | info2a / info3a / info4a
info1b | info2b / info3b / info4b
some subtitle 2
info1c | info2c / info3c / info4c
info1d | info2d / info3d / info4d
...

Is there a way to delete all the subtitle rows and then split the info columns normally (by | and /)? The subtitles can ideally go in a separate table.

Thus, the result would be:

Table1, where the pipes represent the breaks between the columns:

info1a | info2a | info3a | info4a
info1b | info2b | info3b | info4b
info1c | info2c | info3c | info4c
info1d | info2d | info3d | info4d
...

Table2:

some subtitle 1
some subtitle 2
...

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
John Targaryen
  • 1,109
  • 1
  • 13
  • 29

1 Answers1

3

Slicing

rowsiwant <- c(1,4)
#For the first table by removing the unwanted rows
new_table_data <- old_table[-rowsiwant,]
#Creates the second table 
new_table_labels <- old_table[rowsiwant,]

Or you can try the subset function if your row names are numbers:

#Creates the first table
new_table_labels <- subset(old_table, rownames(old_table) != rowsiwant)
#Creates the second table
new_table_data <- subset(old_table, rownames(old_table) == rowsiwant)

Also, there are plenty of great resources for slicing such as: http://statmethods.net/management/subset.html http://statistics.ats.ucla.edu/stat/r/faq/subset_R.htm

Also previous questions are a good resource:

How can I subset rows in a data frame in R based on a vector of values?

Subset of table in R using row numbers?

Community
  • 1
  • 1
Cody Glickman
  • 514
  • 1
  • 8
  • 30
  • What if I wanted to cut out more rows? Like 1, 4, 7, 10 ... 1003, how would I remove those rows? Would I have to type in each number, or is there a simpler way? – John Targaryen Jan 03 '16 at 16:50
  • You could add each row individually, though if you have a pattern (say every third row) of labels you can slice accordingly. I would recommend a grep for rows with subtitles (if they have the same subtitle) http://stackoverflow.com/questions/21311386/using-grep-to-help-subset-a-data-frame-in-r – Cody Glickman Jan 04 '16 at 20:38
  • Also, http://stackoverflow.com/questions/19198492/subset-data-frame-by-complex-pattern-of-column-names & http://stackoverflow.com/questions/19417674/subset-with-pattern are good examples of regex – Cody Glickman Jan 04 '16 at 20:42