0

I have what I suspect is a simple data reformatting question. The data file (txt) is structured with the observation numbers on separate lines,

1
45 65
78 56
2
89 34
39 55

The desired output is,

1 45 65
1 78 56
2 89 34
2 39 55

Suggestions on how to make that conversion would be most appreciated. Thanks.

4

1 回答 1

1

We could read the file with readLines. Create an index variable and split the 'lines'. Remove the first element of the list elements, use read.table to read the file, and unnest

 lines <- readLines('file.txt')
 library(stringr)
 #remove leading/lagging spaces if any 
 lines <- str_trim(lines) 
 #create the index mentioned above based on white space 
 indx  <- !grepl('\\s+', lines)
 #cumsum the above index to create grouping
 indx1 <- cumsum(indx)
 #split the lines with and change the names of the list elements 
 lst <- setNames(split(lines, indx1), lines[indx])
 #Use unnest after reading with read.table 
 library(tidyr)
 unnest(lapply(lst, function(x) read.table(text=x[-1])), gr)
 #   gr V1 V2
 #1  1 45 65
 #2  1 78 56
 #3  2 89 34
 #4  2 39 55

Or we can use Map from base R approach

 do.call(rbind,Map(cbind, gr=names(lst), 
             lapply(lst, function(x) read.table(text=x[-1]))))
于 2015-07-16T20:42:25.677 回答