pranav nerurkar
pranav nerurkar

Reputation: 648

Text processing in R

I have a text file with many lines (first two are shown below)

1: 146 189 229 
2: 191 229

I need to convert to the output

1 146
1 189
1 229
2 191
2 229

I have read the lines in loop, removed the ":" and split by " ".

fbnet <- readLines("0.egonet")

for (line in fbnet){  
  line <- gsub(":","",line)
  line <- unlist(strsplit(line, " ", fixed = TRUE),use.names=FALSE)
  friend = line[1] 
 }

How to proceed next

Upvotes: 4

Views: 72

Answers (1)

akrun
akrun

Reputation: 887971

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")

Upvotes: 4

Related Questions