Mike V
Mike V

Reputation: 1354

An approach to read data table

I am searching for a way where I can read these line of texts into df in a column so that I can do practicing on it.

some text sample 1
some text sample  2 3
some more text 's sample here and then
another one
How to add text to df using read.table

Output:

                                   text
1                     some text sample 1
2                  some text sample  2 3
3    some more text 's sample here and then
4                            another one
5 How to add text to df using read.table

Thank for helping me out.

Upvotes: 1

Views: 46

Answers (2)

akrun
akrun

Reputation: 887038

We could use readLines to read each line and then convert to data.frame. In this way, we can be sure that it will read a line as a whole and doesn't depend upon any separation (in case if present)

data.frame(text = readLines('file.txt'))
#                                    text
#1                     some text sample 1
#2                  some text sample  2 3
#3 some more text 's sample here and then
#4                            another one
#5 How to add text to df using read.table

We can also use read.table with a different sep

df <- read.table('file.txt', header = FALSE, col.names = 'txt', sep=",")

Upvotes: 1

Duck
Duck

Reputation: 39595

You can also try:

#Code
df <- read.delim(file='myfile.txt',header=F,stringsAsFactors = F,sep='\t')

Output:

df
                                      V1
1                     some text sample 1
2                  some text sample  2 3
3 some more text 's sample here and then
4                            another one
5 How to add text to df using read.table

Upvotes: 1

Related Questions