Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

How to convert "heading" rows into new columns

$
0
0

I have data (imported imperfectly from a PDF) that has everything in a single column, with certain rows as descriptive headers. For example:

dfx <- data.frame(V1 = c("Box 1", "abcd10", "bcde15", "Box 2", "cdefg35", "jklm40", "nopq50", "rstu52"))

       V1
1   Box 1
2  abcd10
3  bcde15
4   Box 2
5 cdefg35
6  jklm40
7  nopq50
8  rstu52

I want to create a separate column where each observation takes on the value of the nearest heading above it. Like this:

       V1    v2
1  abcd10 Box 1
2  bcde15 Box 1
3 cdefg35 Box 2
4  jklm40 Box 2
5  nopq50 Box 2
6  rstu52 Box 2

Nothing I've tried has gotten me close. Any help would be appreciated. Thanks!


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>