R - as.numeric(rownames()) for character

Viewed 33

I have a large character with more than 1000 rows of data that looks like:

[5] "<div class=\"field field-name-title-field field-type-text field-label-hidden\"><div class=\"field-items\"><div class=\"field-item even\"><h2>Seminar on The Future of Work: Automation and Employment</h2>\n</div></div></div><div class=\"field field-name-body field-type-text-with-summary field-label-hidden\"><div class=\"field-items\"><div class=\"field-item even\" property=\"content:encoded\"><p>The Social Situation Monitor (SSM) team is inviting policy-makers and researchers to discuss The Future of Work on 10 November, at The Square Meeting Centre, in Brussels.</p>\n<p>The world of work is going through a revolution. But how will this revolution affect our economy and society? Some say that digitisation, artificial intelligence and robots will create more and better jobs, prosperity and more convenient services. Others argue that it will only benefit highly skilled workers, larger businesses and more affluent consumers. </p>\n<p>The SSM team invites policy-makers and researchers to join our panel, consider the evidence and discuss its implications.  </p>\n<p>Panel:</p>\n<ul><li>Grace Lordan, London School of Economics and Political Science</li>\n<li>Wolfgang Dauth, University of Würzburg</li>\n<li>Marco Vivarelli, Università Cattolica del Sacro Cuore</li>\n<li>Ulrich Zierahn, ZEW</li>\n<li>Federico Biagi, JRC</li>\n</ul><p>This event has limited capacity. To request a ticket, please contact <a href=\"mailto:a.vaganay@lse.ac.uk\">a.vaganay@lse.ac.uk</a>.</p>\n</div></div></div><div class=\"field field-name-field-event-date field-type-datetime field-label-inline clearfix\"><div class=\"field-label\">Date:&nbsp;</div><div class=\"field-items\"><div class=\"field-item even\"><span class=\"date-display-single\" property=\"dc:date\" datatype=\"xsd:dateTime\" content=\"2017-11-10T00:00:00+01:00\">10/11/2017</span></div></div></div><div class=\"field field-name-field-location field-type-text field-label-inline clearfix\"><div class=\"field-label\">Location:&nbsp;</div><div class=\"field-items\"><div class=\"field-item even\">The Square Meeting</div></div></div><div class=\"field field-name-field-countries field-type-taxonomy-term-reference field-label-inline clearfix\"><div class=\"field-label\">Country:&nbsp;</div><div class=\"field-items\"><div class=\"field-item even\"><ul class=\"shs-hierarchy\"><li class=\"shs-term-selected first last\">Belgium</li>\n</ul></div></div></div>"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
[6] "<div class=\"field field-name-title-field field-type-text field-label-hidden\"><div class=\"field-items\"><div class=\"field-item even\">Skillsnet e-bulletin: October 2017</div></div></div><div class=\"field field-name-field-date field-type-datetime field-label-hidden\"><div class=\"field-items\"><div class=\"field-item even\"><span class=\"date-display-single\" property=\"dc:date\" datatype=\"xsd:dateTime\" content=\"2017-10-25T00:00:00+02:00\">Wednesday, October 25, 2017</span></div></div></div><div class=\"field field-name-field-summary field-type-text-with-summary field-label-hidden\"><div class=\"field-items\"><div class=\"field-item even\"><p>The SKILLSNET e-bulletin October 2017 Issue has just been send to skillsnet members and published here in a pdf format.</p>\n</div></div></div><div class=\"field field-name-body field-type-text-with-summary field-label-hidden\"><div class=\"field-items\"><div class=\"field-item even\" property=\"content:encoded\"><p><a href=\"http://www.cedefop.europa.eu/en/news-and-press/news/skillsnet-e-bulletin-october-2017\" target=\"_blank\"><img alt=\"\" src=\"http://www.cedefop.europa.eu/files/images/skillsnet_october_2017.png\" /></a></p>\n<p>The highlight of the October 2017 issue of the Skillsnet e-bulletin refers to the new blog article of <a href=\"http://skillspanorama.cedefop.europa.eu/en/expert/konstantinos-pouliakas\" target=\"_blank\">Konstantinos Pouliakas</a> titled \"<a href=\"http://skillspanorama.cedefop.europa.eu/en/blog/making-labour-market-and-skills-intelligence-policy-relevant-how-cedefop-supports-countries\" target=\"_blank\">Making labour market and skills intelligence policy relevant: How Cedefop supports countries</a>\" which is published on the Skills Panorama.</p>\n<p>In the challenging economic climate that followed the global financial crisis, the objective of strengthening links between education and training and the labour market became an increasingly important policy priority. Developing tools and systems to promote better anticipation and matching of skills to labour market needs is identified as a core part of this strategy and is also integral to the European Commission’s New Skills Agenda for Europe. Continue reading <a href=\"http://skillspanorama.cedefop.europa.eu/en/blog/making-labour-market-and-skills-intelligence-policy-relevant-how-cedefop-supports-countries\" target=\"_blank\">here</a>.</p>\n<p>In order to receive each month, the SKILLSNET e-bulletin in your mailbox subscribe to the SKILLSNET e-bulletin <a href=\"https://www.cedefop.europa.eu/en/user/register?current=\">here</a>.</p>\n</div></div></div><div class=\"field field-name-field-source-link field-type-link-field field-label-hidden\"><div class=\"field-items\"><div class=\"field-item even\"><a href=\"http://www.cedefop.europa.eu/en/news-and-press/news/skillsnet-e-bulletin-october-2017\" target=\"_blank\">Download the Skillsnet e-bulletin: October 2017 here</a></div></div></div>" 

I would like to get the row numbers, I am using the following command:

as.numeric(rownames(character_clean))

but I get

> as.numeric(rownames(character_clean))
numeric(0)

which is not what I want. How can I get what I want?

1 Answers

This should work for you:

library(dplyr)

character_clean <- data.frame(
matrix(
  c(
    '<div class=\"field field-name-title-field field-type-text field-label-hidden\"><div class=\"field-items\"><div class=\"field-item even\"><h2>Seminar on The Future of Work: Automation and Employment</h2>\n</div></div></div><div class=\"field field-name-body field-type-text-with-summary field-label-hidden\"><div class=\"field-items\"><div class=\"field-item even\" property=\"content:encoded\"><p>The Social Situation Monitor (SSM) team is inviting policy-makers and researchers to discuss The Future of Work on 10 November, at The Square Meeting Centre, in Brussels.</p>\n<p>The world of work is going through a revolution. But how will this revolution affect our economy and society? Some say that digitisation, artificial intelligence and robots will create more and better jobs, prosperity and more convenient services. Others argue that it will only benefit highly skilled workers, larger businesses and more affluent consumers. </p>\n<p>The SSM team invites policy-makers and researchers to join our panel, consider the evidence and discuss its implications.  </p>\n<p>Panel:</p>\n<ul><li>Grace Lordan, London School of Economics and Political Science</li>\n<li>Wolfgang Dauth, University of Würzburg</li>\n<li>Marco Vivarelli, Università Cattolica del Sacro Cuore</li>\n<li>Ulrich Zierahn, ZEW</li>\n<li>Federico Biagi, JRC</li>\n</ul><p>This event has limited capacity. To request a ticket, please contact <a href=\"mailto:a.vaganay@lse.ac.uk\">a.vaganay@lse.ac.uk</a>.</p>\n</div></div></div><div class=\"field field-name-field-event-date field-type-datetime field-label-inline clearfix\"><div class=\"field-label\">Date:&nbsp;</div><div class=\"field-items\"><div class=\"field-item even\"><span class=\"date-display-single\" property=\"dc:date\" datatype=\"xsd:dateTime\" content=\"2017-11-10T00:00:00+01:00\">10/11/2017</span></div></div></div><div class=\"field field-name-field-location field-type-text field-label-inline clearfix\"><div class=\"field-label\">Location:&nbsp;</div><div class=\"field-items\"><div class=\"field-item even\">The Square Meeting</div></div></div><div class=\"field field-name-field-countries field-type-taxonomy-term-reference field-label-inline clearfix\"><div class=\"field-label\">Country:&nbsp;</div><div class=\"field-items\"><div class=\"field-item even\"><ul class=\"shs-hierarchy\"><li class=\"shs-term-selected first last\">Belgium</li>\n</ul></div></div></div>',                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
    '<div class=\"field field-name-title-field field-type-text field-label-hidden\"><div class=\"field-items\"><div class=\"field-item even\">Skillsnet e-bulletin: October 2017</div></div></div><div class=\"field field-name-field-date field-type-datetime field-label-hidden\"><div class=\"field-items\"><div class=\"field-item even\"><span class=\"date-display-single\" property=\"dc:date\" datatype=\"xsd:dateTime\" content=\"2017-10-25T00:00:00+02:00\">Wednesday, October 25, 2017</span></div></div></div><div class=\"field field-name-field-summary field-type-text-with-summary field-label-hidden\"><div class=\"field-items\"><div class=\"field-item even\"><p>The SKILLSNET e-bulletin October 2017 Issue has just been send to skillsnet members and published here in a pdf format.</p>\n</div></div></div><div class=\"field field-name-body field-type-text-with-summary field-label-hidden\"><div class=\"field-items\"><div class=\"field-item even\" property=\"content:encoded\"><p><a href=\"http://www.cedefop.europa.eu/en/news-and-press/news/skillsnet-e-bulletin-october-2017\" target=\"_blank\"><img alt=\"\" src=\"http://www.cedefop.europa.eu/files/images/skillsnet_october_2017.png\" /></a></p>\n<p>The highlight of the October 2017 issue of the Skillsnet e-bulletin refers to the new blog article of <a href=\"http://skillspanorama.cedefop.europa.eu/en/expert/konstantinos-pouliakas\" target=\"_blank\">Konstantinos Pouliakas</a> titled \"<a href=\"http://skillspanorama.cedefop.europa.eu/en/blog/making-labour-market-and-skills-intelligence-policy-relevant-how-cedefop-supports-countries\" target=\"_blank\">Making labour market and skills intelligence policy relevant: How Cedefop supports countries</a>\" which is published on the Skills Panorama.</p>\n<p>In the challenging economic climate that followed the global financial crisis, the objective of strengthening links between education and training and the labour market became an increasingly important policy priority. Developing tools and systems to promote better anticipation and matching of skills to labour market needs is identified as a core part of this strategy and is also integral to the European Commission’s New Skills Agenda for Europe. Continue reading <a href=\"http://skillspanorama.cedefop.europa.eu/en/blog/making-labour-market-and-skills-intelligence-policy-relevant-how-cedefop-supports-countries\" target=\"_blank\">here</a>.</p>\n<p>In order to receive each month, the SKILLSNET e-bulletin in your mailbox subscribe to the SKILLSNET e-bulletin <a href=\"https://www.cedefop.europa.eu/en/user/register?current=\">here</a>.</p>\n</div></div></div><div class=\"field field-name-field-source-link field-type-link-field field-label-hidden\"><div class=\"field-items\"><div class=\"field-item even\"><a href=\"http://www.cedefop.europa.eu/en/news-and-press/news/skillsnet-e-bulletin-october-2017\" target=\"_blank\">Download the Skillsnet e-bulletin: October 2017 here</a></div></div></div>'
  ),
  nrow = 2,
  ncol = 1,
  byrow = TRUE,
  dimnames = list(NULL,
                  c("text_column"))
),
stringsAsFactors = FALSE
)

character_clean_new <- character_clean %>% 
  mutate(row_num = row_number())

It's really the same suggestion as one of the comments, but I would avoid naming the new column "row". row is a function in base R.

Related