Here is an example on reading a data file that contains a column with text blocks from a file that has commas in the comment/text block and possibly also has carriage returns in a long text block.
The example data file has a column called opinion
. For
subject ID 4, their opinion
comment includes not only some
commas but also has a carriage (line) return about halfway.
Here is a screenshot of this from EXCEL - see the text block for ID 4 - notice that there are 2 lines for this subject’s text block - that is because there is a carriage return after “and”.
Download these 2 “textwrap” datasets:
For the CSV formatted file, you can read in the datafile using the
readr
package with readr::read_csv()
function.
# read in as a CSV file
library(readr)
textwrap <- read_csv("textwrap.csv")
textwrap
## # A tibble: 5 × 4
## id age height opinion
## <dbl> <dbl> <dbl> <chr>
## 1 1 23 5.6 "I really liked the course, but not the slides"
## 2 2 34 6.1 <NA>
## 3 3 33 6.3 "it was ok"
## 4 4 28 5.8 "I'd love to see some new dataset, and \nperhaps, some new…
## 5 5 31 5.9 "need more data, charts, and help docs"
You’ll notice that the opinion
block of text has
\n
which indicates that there is a carriage return (new
line) right before the word perhaps
.
For the XLSX (EXCEL) formatted file, you can read in the datafile
using the readxl
package with
readr::read_excel()
function.
#read in from EXCEL
library(readxl)
textwrap2 <- read_excel("textwrap.xlsx")
textwrap2
## # A tibble: 5 × 4
## id age height opinion
## <dbl> <dbl> <dbl> <chr>
## 1 1 23 5.6 "I really liked the course, but not the slides"
## 2 2 34 6.1 <NA>
## 3 3 33 6.3 "it was ok"
## 4 4 28 5.8 "I'd love to see some new dataset, and \r\nperhaps, some n…
## 5 5 31 5.9 "need more data, charts, and help docs"
You’ll notice that the opinion
block of text has
\r\n
which indicates that there is a carriage return (new
line) right before the word perhaps
.
So, there are some slight differences in the final output for how the carriage return characters are captured, but both approaches work.