Home > Enterprise >  Read CSV files as a single string in R
Read CSV files as a single string in R

Time:10-04

I have a couple of .csv files that contain strings of text. I need a way for RStudio to store the string of texts and their file names as a dataFrame.

uniqueName_KIwwciwldv.csv contains Hello, I, contain, some numbers like, 12, 10.321

What I want is that I should be able to create a single dataframe with column names like:

head(df)

uniqueID                 text
uniqueName_KIwwciwldv    "Hello, I, contain, some numbers like, 12, 10.321"  

I have tried to read csv but then it assigns individual values to columns (not something I want).

CodePudding user response:

Maybe some combination of list.files and readLines could help

csv_files <- list.files(pattern = "\\.csv$")
Text <- purrr::map_chr(csv_files, readLines)

data.frame(UniqueID = gsub("\\.csv", "", csv_files), 
       text=Text)
                                         text
1         uniqueName_KIwwciwldv    Hello, I, contain, some numbers like, 12, 10.321
2 uniqueName_KIwwciwldv_letters    Hello, I, contain, some letters like, A, B, C
  • Related