How to import a dataframe into R?

Coding in R

To read-in a DataFrame from a file on your desktop into R, you can use the read.csv() function for CSV files, read.table() for text files, or specific functions for other file formats. Here’s a general guide:

Set Working Directory

VBA Code Display
setwd("path/to/your/desktop")

Import Data

VBA Code Display
df = read.csv("your_file.csv")
df <- read.table("your_file.txt", header = TRUE, sep = "\t")

To read a DataFrame from an Excel file into R, you can use the readxl package, which is a popular package for reading Excel files. If you don't have it installed, you can install it using the following command:

VBA Code Display
install.packages("readxl")

After installing the package, you can use the read_excel() function to read the Excel file into a DataFrame. Here's an example:

VBA Code Display
# Install and load the readxl package
install.packages("readxl")
library(readxl)
            
# Set working directory to the desktop
setwd("path/to/your/desktop")
            
# Read Excel file from desktop
df <- read_excel("your_file.xlsx")
            
# Display the DataFrame
print(df)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top