Importing a text file

Hello everyone,

I'm a new R user. I've just tried to import text file in R. When I used 'import dataset' it opened columns but every value were 'N/A'. Then I used to import by code. I used the following code
ds<-read.table("C:/Path/name.txt",header=TRUE,sep=","), but it opens just one column where values are not shown again. It's opened nicely in excel but not in R.
Could you please suggest any solution to this problem?
Thank you in advance.

Hello,

Welcome to the forum.

The easiest way to read a .txt file is with the following:

library(readr)

readr::read_delim("file.txt", delim = "|")

Just be sure to specify the right delimiter. Let me know if this solves your problem :slight_smile:

As I see it's comma. When I opened it in excel, I used comma as a delimiter and it worked so I use comma as a delimiter in R as well but it does not work even when I use read_delim function.

Can you share your file or a portion of it so I can reproduce on my side?

Can you also check if your data starts from the first row & column or do you have some merged cells or some additional texts at the start of the excel sheet?

Most likely the function read.table or readr::read_* is not able to find the delimiter and hence getting your data in the first column.

Excel merged cells and hidden rows/columns have tripped me multiple times in the past ! :slight_smile:

Thank you for your time, it was because of the last line, when I deleted it, it imported well. Maybe it was formatted not accurately

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.