R tutorial: read.delim & read.table

Learn how to import flat-file data into R using read.delim for tab-separated files and read.table for custom delimiters. Set header=TRUE for column names, sep for your delimiter, and stringsAsFactors=FALSE to keep text as strings.
Chapters
In a previous video you learned about read CSV, which is an R function that specifically exists for importing CSV data. However, flat file data doesn't come as comma separated values alone. Another common format of flat file data is the tab delimited file like this stage. Txt with the same data as before. To import it, we'll need read the lim this time. As usual, you simply specify the path to the file, and also that we want to import strings as strings and not as factors. Works like a charm. If your data comes in this typical comma separated or tab delimited format, your life is easy and importing the data is a walk in the park. If it's not, you'll have to do some more customization work. Say we have the same states data again, but this time the values are separated by a forward slash instead of commas or tabs in the file. States 2. TXT how to go about this? Read CSV and read the lim won't do you much good in this case. Here you'll want to use read table. It's the main importing function in the utils package, allowing you to read in any file in table format and create a dataframe from it. The number of arguments you can specify for this function is huge, so I won't go through each and every one of these arguments. Instead, let's have a look at the retold tablecall that imports states 2. Txt and try to understand what happens. As usual, the first argument is the path to the file you want to import. The header argument is something that we haven't seen before. If you set this to true, you tell R that the first row of the text file contains the variable names, which is the case here. Read table sets this argument to false by default, which would mean that the first row is always read as an observation, even if it's a row of variable names. Next, sep is the argument that specifies how fields in a record are separated for our file. Here the field separator is a forward slash like this. Finally, there's again the strings as factors argument, which we set to false. Again, because we want to import strings as strings, the result looks just as we'd want. Nice. Apart from the arguments we discussed here, there are also ways to specify column names and column types, among others. Get some practice on them in the exercises.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







