简体   繁体   中英

Converting text data to table/csv format

I need to convert text data (eg, paragraph) into a dataframe (to save as a csv file) using R. The specific need is to have each word in each paragraph in a separate cell in a column. The following code converts the text into a table, but it puts words in each line in a single cell. Can you help create a single-column dataset with each word in a separate cell?

    merchant <- read.delim("merchant.txt")
    write.table(merchant,file="merchant.csv",sep=",",col.names=FALSE,row.names=FALSE)

Here's my attempt based on tidyverse. Instead of reading in as a table, just read in as a string and then separate into a vector of individual words:

library(tidyverse)

## Read in text file as string
merchant <- read_file("merchant.txt") %>% 
## Remove all punctuation
gsub('[[:punct:] ]+',' ',.) %>%
## Split individual words into list vector
strsplit(" ")
## Set column equal to the vector of individual words
para <- merchant[[1]]

To convert this into a dataframe:

para <- as.data.frame(para)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM