read a file in r

To read a file in R, you can use the read.table() or read.csv() functions depending on the file format. Here's an example of how to read a CSV file:

main.r
# Set the file path
file_path <- "path/to/your/file.csv"

# Read the file using read.csv()
data <- read.csv(file_path)

# View the data
head(data)
147 chars
9 lines

Alternatively, you can use the read.table() function to read in other file formats such as tab-separated files:

main.r
# Set the file path
file_path <- "path/to/your/file.txt"

# Read the file using read.table() with sep argument set to "\t"
data <- read.table(file_path, header = TRUE, sep = "\t")

# View the data
head(data)
208 chars
9 lines

Note that the header argument specifies whether the first row of the file is a header (column names) or not. If the file does not have a header row, you can set header = FALSE.

gistlibby LogSnag