Home > Blockchain >  Read lines from txt file and save in Dataframe (create new row every 4 lines)
Read lines from txt file and save in Dataframe (create new row every 4 lines)

Time:11-27

I am currently trying to read a .txt file in to a data.frame. I want to save my .txt file in to a data.frame with four columns. I want to save each four lines of the file into one row. For example:

Txt-file:

 - A
 - B
 - C
 - D
 - E
 - F
 - G
 - H

should result in:

df: 

1  A|B|C|D
    
2  E|F|G|H

CodePudding user response:

A dplyr and tidyr solution, which takes into account that the text file may not be length factor 4...

# assume your text file is a vector

df <- data.frame(txt = LETTERS[1:18])

library(dplyr)
library(tidyr)

# add NA rows if df is not a factor of 4

df <- bind_rows(df, data.frame(txt = rep(NA_character_, nrow(df) %% 4)))

  df %>%
    mutate(col = rep(paste0("col_", 1:4), nrow(df) / 4),
           id = rep(seq_len(nrow(df) / 4), each = 4)) %>%
    pivot_wider(names_from = col, values_from = txt)
#> # A tibble: 5 x 5
#>      id col_1 col_2 col_3 col_4
#>   <int> <chr> <chr> <chr> <chr>
#> 1     1 A     B     C     D    
#> 2     2 E     F     G     H    
#> 3     3 I     J     K     L    
#> 4     4 M     N     O     P    
#> 5     5 Q     R     <NA>  <NA>

Created on 2021-11-25 by the reprex package (v2.0.1)

  • Related