Importing .CSV file saved in my laptop into R

Hello,
I would greatly appreciate if you can help me to imort .csv file saved in my laptop into R.
I am using the following code but I keep getting error

read_csv("C:\Users\Name of account\location in laptop\name.csv")

Please can you tell me what I am doing wrong

1 Like

Move the file into your current working directory, the one shown in the file pane or with the getwd() command. The use the same function with just the name of the file.

1 Like

An alternative to @technocrat's answer is to use forward slashes in the file path. This does work on Windows systems.

read_csv("C:/Users/Name of account/location in laptop/name.csv")
1 Like

Thank you for your response. Can you please how to do so if I have the .csv file in the Downloads folder in my laptop?

I assume @FJCC suggestion would work, but it's been 20 years since I've had to use windows.

For find the correct path you could use file.choose() and select the file and the console show the path.

file.choose() # select the file and press open.

sewd() is redundant if you're using an absolute path.

Important:
run/check this code:
message("a\n\b")
message("a\tb")
message("a\b\b")
look at: R Strings - Escape Characters

Use file.path() (e.g file.path("C:", "Users", "WindowsLogin", "Subdir", "your.file.csv") ).
Good luck!

I am still not able to add .csv file from my Downloads folder in my laptop to R. I am new to R, so I would appreciate if anyone can guide me step by step how to add it. I tried all above suggestion but nothing works.

I am wondering why it is complicated to transfer file from laptop/desktop to R. I feel I am doing something wrong while writing the above codes. Thank you

It really is quite straightforward
You could post your error messages and code - but the most likely explanation is that you are using Windows backslash instead of R slash

One approach is to use RStudio's Import Dataset (probably top right of screen) and choose From Text. That will allow you to Browse your files to choose the file and generate the code

1 Like

What makes it seem complicated (which it isn't very) could be one of two things: the concept of a file system or how files are identified.

The first might seem silly, but I've heard uni-level instructors notice that the idea of a file has been harder to grasp now that the paper-file analogy on which it is based is rapidly disappearing from daily life. Around 1940 or so, when computer data in the form of a deck of punch cards was first described as a "file" everyone who worked in an office knew exactly what analogy was being made.

Before mail became devoted mainly to bills, catalogs, charitable solicitation, advertising and political campaigns people would write letters. When email became popular, we started referring to communications through the mail service as "snail mail" because it didn't arrive shortly after being sent. It could takes days. But everyone used it because telephone calls were expensive and there wasn't always someone to take a message, voice mail was unheard of, and telephoning could be expensive. So, people wrote out their thoughts on paper.

This was particularly true in office culture. Here is an example of a "memorandum to file"

And to find it again, it was put into a file folder

image

and to store the file folder, it was put in a file cabinet

image

The organization of documents (files) within file folders within file cabinets ranged from "just throw it in somewhere" to file alphabetically by something to elaborate systems based on date, sender, receiver, subject, organizational unit, cabinet number, file room number, etc., depending on the office culture.

That seemed a natural mental model for documents that were kept in computer systems.

A hard disk is conceptually close to a filing cabinet, a directory is conceptually close to a folder, and a file is conceptually close to a paper document.

There remains how to map this from the physical office version to the world of laptops and desktops. The way shared by the three major operating systems, Windows, Linux and macOS is hierarchical.

There is a "root" folder below which all folders are organized

/

On the system I'm writing on, / has several sub-directories

➜ ls
bin@   home/   libx32@      nix/       root/  srv/               usr/
boot/  lib@    lost+found/  opt/       run/   sys/               var/
dev/   lib32@  media/       proc/      sbin@  tmp/
etc/   lib64@  mnt/         recovery/  snap/  upgrade-attempted

and each of those has sub-directories like my home folder

basket/       fonts/       nb.held/           perl5/     stub
bin/          include/     nb.hld/            Pictures/  target/
bucket        index.hld    Nextcloud/         plan       texmf/
config/       lib/         node_modules/      projects/  tmp/
Desktop/      linuxbrew@   ol                 Public/    Videos/
Documents/    mean         package.json       R/         volumes.jl
Downloads/    mk_jproj.sh  package-lock.json  snap/      z.sublime-project
_extensions/  Music/       pandoc-server.1    src/

and so on.

I've got 65,710 directories containing 462,419 files currently in my home directory. How do I keep them straight?

As an example, I have an R script /home/roc/projects/demo/tsp.R which is under a demo directory under a projects directory under my personal roc directory under the home directory of users under the / root directory.

If I have tsp.R open in RStudio and I want to bring in a csv file from my Desktop directory, I have to provide the full name.

d <- read.csv("/home/roc/Desktop/newdata.csv")

but if the data I wanted was already in the demo directory, just

d <- read.csv("newdata.csv')

So, for you to continue you need to find out how to express in Windows the full formal name of the csv file in your Desktop directory. It needs to be quoted, like my examples. If the full filename were like

C:\Users\Sabouchaer\Desktop\newdata.csv

it still needs, in addition to being quoted, some tweaking to account for Window's use of the backslash \

C:\\Users\\Sabouchaer\\Desktop\\newdata.csv

Putting it all together, and assuming that your username and file name are as shown and you are on the C: drive and, importantly the file is actually located there try this with appropriate adjusments

d <- read.csv("C:\\Users\\Sabouchaer\\Desktop\\newdata.csv")

I can't test this because I don't have a Windows box, but it should work. If it doesn't, you should probably find someone local in person to show you where you are losing the way.

1 Like

Im never know this story, amaizing @technocrat.
Allways is better know the things.

Just to make sure, do you have R and RStudio installed on your laptop or are you using Posit Cloud?

I suspect the latter based on your question "I am wondering why it is complicated to transfer file from laptop/desktop to R." In this case, you need to click on the Files tab in the lower right pane then click on Upload. Click on Choose File, find your .csv file, select it and click on Open. Click on OK.

If the former, I suggest you go to the File menu, click on Import Dataset and choose the From Text (readr) option. A window will open with a Browse... button in the upper right corner. Click on the button and then navigate to your .csv file. Select it and click on Open. The code to read that file will appear in the lower right corner of the window under Code Preview

2 Likes

Thank you all for responses and help

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.