0

I'm extremely new to using R and I keep running into an issue with my current data set. The data set consists of several txt files with 30 rows and 3 columns of numerical data. However, when I try to work with them in r, it automatically makes the first row of data the column heading, so that when I try to combine the files, everything gets messed up as none of them have the same column titles. How do I stop this problem from happening? The code I've used so far is below!

setwd("U:\\filepath")
library(readr)
library(dplyr)
file.list <- list.files(pattern='*.txt')
df.list <- lapply(file.list, read_tsv)

After this point it just says that there are 29 rows and 1 column, which is not what I want! Any help is appreciated!

aynber
  • 22,380
  • 8
  • 50
  • 63
Psych
  • 3
  • 1
  • Does this answer your question: https://stackoverflow.com/questions/23209330/how-to-change-the-first-row-to-be-the-header-in-r – Yash Laddha Jul 12 '22 at 21:27
  • 2
    You can usually get help on a function by doing, e.g. `?read_tsv` , which will show you all the options you can change in the "Arguments" section. – thelatemail Jul 12 '22 at 21:32
  • @YashLaddha : Psych is asking for a solution of the inverse problem. He needs to make up names, but only after he solves the other problem of determining what delimiter is being used. – IRTFM Jul 12 '22 at 22:16

2 Answers2

1

Use df_list <- lapply(file.list, read_tsv, col_names = FALSE).

WhatIf
  • 626
  • 4
  • 10
1

You say:

After this point it just says that there are 29 rows and 1 column, which is not what I want!

What that is telling you is that you don't have a tab-separated file. There's not a way to tell which delimiter is being assumed, but it's not a tab. You can tell that by paying attention to the number of columns. Since you got only one column, the read_tsv function didn’t find any tabs. And then you have the issue that your colnames are all different. That well could mean that your files do not have a header line. If you wanted to see what was in your files you could do something like:

 df.list <- lapply(file.list, function(x) readLines(x)[1])
 df.list[[1]]

If there are tabs, then they should reveal themselves by getting expanded into spaces when printed to the console.

Generally it is better to determine what delimiters exist by looking at the file with a text editor (but not MS Word).

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks for your help! I looked into it and the files appear to be space delimited rather than tab. Do you know how I could add a header line? – Psych Jul 13 '22 at 13:29