1

enter image description hereI am trying to read a txt file into R environment. I do not understand what is the problem with it and why I cannot read the txt into columns. Can someone explain to me what is the actual problem here? Because that will enable me to search properly on internet. But first I have to understand the issue of it.

This is the type of data I have.

structure(list(dateRep.day.month.year.cases.deaths.countriesAndTerritories.geoId.countryterritoryCode.popData2018.continentExp = c("01/06/2020,1,6,2020,680,8,Afghanistan,AF,AFG,37172386,Asia", 
"31/05/2020,31,5,2020,866,3,Afghanistan,AF,AFG,37172386,Asia", 
"30/05/2020,30,5,2020,623,11,Afghanistan,AF,AFG,37172386,Asia", 
"29/05/2020,29,5,2020,580,8,Afghanistan,AF,AFG,37172386,Asia", 
"28/05/2020,28,5,2020,625,7,Afghanistan,AF,AFG,37172386,Asia", 
"27/05/2020,27,5,2020,658,1,Afghanistan,AF,AFG,37172386,Asia"
)), row.names = c(NA, 6L), class = "data.frame")

I have tried to read this table into R with this code:

test <- read.delim("download.txt", header = TRUE, sep = "\t")

And this code:

read.table(download.txt, header = TRUE, sep = ",")

Is there a tidyverse way of reading the txt file with separated columns?

GaB
  • 1,076
  • 2
  • 16
  • 29
  • What issue are you running into when doing this? What error does R give you? Note in the last line of code you provide, it should probably be `"download.txt"` instead of `download.txt`. Your data appear to be comma-delimited -- so you can use `read.csv` directly. – bzki Jun 04 '20 at 20:10
  • As mentioned above. The when reading the txt file, it does not read into columns. I thought the codes above will do the job. I do not get any error. But simply put it, do not know why it does not read into columns. – GaB Jun 04 '20 at 20:12
  • `dateRep.day.month.year.cases.deaths.countriesAndTerritories.geoId.countryterritoryCode.popData2018.continentExp` is treated as a single column when you read this in, if you look at the output of the data.frame -- so `R` thinks you are just reading in a single column of data. Normally each column name should be separated by a comma. I'm a little unclear what the data looks like in "download.txt" -- that might help us resolve how to read this in correctly – bzki Jun 04 '20 at 20:17
  • So, when you deploy the table, it should give the type of data I have? Not sure. But when looking into my actual download I have the columns all in one column and the names of the columns are disociated with a . – GaB Jun 04 '20 at 20:20
  • I added a picture as well, just in case – GaB Jun 04 '20 at 20:22
  • 1
    Can you try `read.csv("download.txt", stringsAsFactors = F)` and report back the output you get? – bzki Jun 04 '20 at 20:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215334/discussion-between-bzki-and-gab). – bzki Jun 04 '20 at 20:28
  • Okay, it did. But because I have took out the extension of the file txt. And then read it as you said. Would you please put this as an answer so that I can tick it? – GaB Jun 04 '20 at 20:29

1 Answers1

1

I suspect the issue was that download.txt should have been "download.txt" in the last line you wrote. Your data is comma-separated, so you can use read.table("download.txt", header = TRUE, sep = ","), or, simply read.csv("download.txt") with default arguments.

bzki
  • 621
  • 4
  • 5