We can read with read.csv/read.txt
specifying the delimiter as :
to output a data.frame with 2 columns and then use separate_rows
to split the second column ('V2' - when we specify header = FALSE
- the automatic naming of columns starts with letter V
followed by sequence of numbers for each column) with space delimiter into separate rows and remove the NA elements (in case there are multiple spaces) with filter
library(tidyverse)
read.csv(text=fbnet, sep=":", header = FALSE) %>%
separate_rows(V2, convert = TRUE) %>%
filter(!is.na(V2))
V1 V2
1 1 146
2 1 189
3 1 229
4 2 191
5 2 229
Or using read_delim
from readr
with separate_rows
and filter
read_delim(paste(trimws(fbnet), collapse="\n"), delim=":", col_names = FALSE) %>%
separate_rows(X2, convert = TRUE) %>%
filter(!is.na(X2))
data
fbnet <- readLines(textConnection("1: 146 189 229
2: 191 229"))
#if we are reading from file, then
fbnet <- readLines("file.txt")