I am trying to read in a .txt file and parse part of it, which is a table, into a data frame. So far I've read the .txt file into r using read_lines
and selecting the rows of the file I want to work with. This leaves me with a character
vector with a length of over 500.
I can convert this into a table using either:
data.frame(str_split_fixed(myStringName, " +", 21))
which separates by white space into 21 columns
k <- read.table(text=myStringName, header = TRUE, fill = TRUE)
The issue is that there are blank spaces in some "cells" of the table where data is not reported. So in instances where not every "cell" has data in a row that data gets shifted to the left and the output looks something like this (I've hidden the first 11 columns which all have data). Here, only rows 4 and 5 are correct, the others get shifted to the left depending on how much is missing.
1 2 3 4 5 6 7 8 9 10
1: -0.71 4 0.5158 0.000 1.000 2 1 CLIM1_sc |
2: 0.18 2 0.8757 1.000 1 CLIM1_sc |
3: -0.33 0 0.0000 1.000 CLIM1_sc |
4: -0.14 45 0.8881 0.157 .6919 .54 9 2 CLIM2_sc |
5: -0.07 3 0.9469 0.500 .4795 .00 2 2 CLIM2_sc |
6: -0.26 2 0.8220 1.000 2 CLIM2_sc |
How can I read this data into a table so that it outputs correctly? I would like the output to look something like this:
1 2 3 4 5 6 7 8 9 10
1: -0.71 4 0.5158 0.000 1.000 2 1 CLIM1_sc |
2: 0.18 2 0.8757 1.000 1 CLIM1_sc |
3: -0.33 0 0.0000 1.000 CLIM1_sc |
4: -0.14 45 0.8881 0.157 .6919 .54 9 2 CLIM2_sc |
5: -0.07 3 0.9469 0.500 .4795 .00 2 2 CLIM2_sc |
6: -0.26 2 0.8220 1.000 2 CLIM2_sc |
I could use a fixed width for this one file, but I would like to do this for multiple files and some of the lengths of the first few columns are different.